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