Thanks everyone for your responses. Felix, your before_save
suggestion sounds promising. But will before_save be able to skip the
save operation if the "do some stuff" fails?
What I want to do is this: always do "some stuff" whenever the x
attribute is updated. But if "some stuff" returns nil which means it
has failed, do not update x, and then also return nil to the call to
update x to let the caller know that the update operation of x has
failed.
I looked up before_save on the API guide and online but still don't
know the answer.
Right now the way I am implementing this is:
def set_x(val)
catch :some_stuff_has_failed do
do_some_stuff # throw :some_stuff_has_failed if it fails
self.x = val
save!
end
end
I try to make the setter "x=" private so that anybody using my class
has to use "set_x" to set a value for x. However, I am getting the
"attempt to call private method" error which causes me to post this
thread.
Please let me know if my description of the problem is still
incomplete.
Thanks all for your help.
On Jul 21, 2:14 am, Felix Schäfer <[email protected]> wrote:
> 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
-~----------~----~----~----~------~----~------~--~---