Callbacks for to-one associations?

To-many associations have those nice before/after_add/remove
callbacks. What’s the best way to hook in custom code for to-one
associations? I want to update a date field every time a certain
belongs_to association gets changed.

Thanks,
Christian

I wrote:

To-many associations have those nice before/after_add/remove
callbacks. What’s the best way to hook in custom code for to-one
associations? I want to update a date field every time a certain
belongs_to association gets changed.

The following seems to do the trick. Is there a more elegant
solution, though?

class Foo < ActiveRecord::Base
belongs_to :bar

alias_method :_bar=, :bar=

def bar=(new_bar)
update_date
self._bar = new_bar
end

Thanks,
Christian

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs