Thanks, yes, that would do the trick.  Can I assume that:

  def email=(address)
    self[:email] = address
  end

is exactly synonymous with the normal accessors rails gives us?
Particularly in regards to side effects and how it interacts with
things like attr_accessible, mass-assignment via params, and so on?

        -glenn

Jordan Fowler wrote:
> Hi Glenn,
> 
> 1. Your rationale is reasonable, considering you may want to work with
> the new obj.email before saving
> 2. Use the write_attribute or the []= synonym to set the actual field
> value, example below:
> 
> def email=(address)
>   self[:email] = address
>   if self.changed.member?("email")
>     self.email_last_sent = nil
>   end
> end
> 
> Hope this helps.
> 
> -Jordan
> 
> On Wed, Nov 12, 2008 at 12:21 PM, Glenn Little <[EMAIL PROTECTED]> wrote:
>> I have a situation where I'd like a change to an AR object's
>> attribute to conditionally cause another attribute change.
>>
>> For instance, class Person has attributes
>>
>>  varchar  name
>>  varchar  email
>>  datetime email_last_sent
>>
>> If the person's email address changes, I want to zero-out the
>> email_last_sent date.
>>
>> I can think of two places to do this:  a before_save filter, or
>> in the actual "email=" accessor.  I'd prefer to use the
>> accessor, since if I use before_save, my objects will be "incorrect"
>> until I do a save()
>>
>> Two questions:
>>
>>  1) is my rationale regarding not wanting to use a before_save filter
>>    reasonable?
>>
>>  2) I can't figure out how to create a modified accessor that basically
>>    wraps the normal ActiveRecord-provided accessor.  I want something
>>    like:
>>
>>    def email=(address)
>>      ar_supplied_email= (address)
>>      if self.changed.member?("email")  # or any other way of knowing if it 
>> changed
>>        self.email_last_sent = nil
>>      end
>>    end
>>
>>    But how do I get at "ar_supplied_email="?  I tried it with "self.email="
>>    but that not too surprisingly overflowed the stack :-)
>>
>> Thanks!
>>
>>        -glenn
>>
>>
>>
>>
> 
> 
> 

--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to