Am 21.07.2009 um 09:40 schrieb Frederick Cheung:
> On Jul 21, 12:02 am, "David A. Black" <[email protected]> wrote: >> You can do this and get, by coincidence, something like the desired >> effect: >> >> def x=(val) >> super >> # do other stuff >> end >> >> because the call to super will trigger AR::B#method_missing, but the >> more I look at it and play around with it, the more I think it's way >> too fragile and too closely couple to the method_missing >> implementation to use. > > I haven't actually tried this but it looks like activerecord won't > generate an accessor if the method already exists (see > define_attribute_methods in activerecord/lib/active_record/ > attribute_methods.rb It's a little hard to really help because of the lack of information regarding the real problem behind the OP's question (such as: why would you want to return a boolean from a setter method, or what is the other stuff that goes in the setter, is it really something that belongs there?), but I think the most sensible thing to do would be to just: def x=(val) # do other stuff write_attribute(x, val) end (off the top of my head, be sure to verify the syntax...) I think the crucial part here is: does this "other stuff" really belong in the setter? Shouldn't it go in some before_save callback for example? Or in an observer? Or maybe even just in another function like: def do_some_stuff_with_and_set_x(val) # do other stuff self.x = val return whatever end My point here being that a setter should really just do that: set an attribute to a certain value. For any validation of sorts, you have validation callbacks, for any sanitizing that needs to be done, you can hook into the before_save callback, and so forth. Felix --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

