Would the after_find callback work?
On Mar 17, 6:36 am, Mike Blyth <[email protected]> wrote: > I need a way to do something when a certain model parameter is changed > in update, and want to do this at the model level. For example, if the > member.spouse_id is deleted, then somewhere during the update/save > process the Member model would make sure that the spouse's spouse_id > is also deleted. Or, if family.location is changed, to change the > location of all the family members. > > The way I see to determine a change is to look at the record before it > is updated and compare with the incoming params that could change it. > For example, compare member.spouse_id with params[:record][:spouse]. > If there is a change, I need to save the original value somewhere so > that the member will "know" what it was when it needs to take action > such as de-linking the old spouse. > > For that, I've made an accessor, previous_spouse. So, in principle, I > can use a callback before the update takes place, and say > "record.previous_spouse = record.spouse". Then a model after_save > callback can take care of doing whatever is needed to the > original_spouse. > > The problem is that I can't find a callback that works, one that is > called after the record has been retrieved but before it has been > updated. I tried (controller) do_update, but the record has not been > retrieved yet. Even if I retrieve and modify it, it will be discarded > and re-read, so the instance variable previous_spouse, not being saved > in the database, will be lost. The other option seems to be > (controller) before_update_save, but that's called after the record > has been modified, so the original value is already lost; I suppose it > could be retrieved from the database since the updated record has not > yet been saved, but that seems awkward. > > Any ideas? Or am I missing a much simpler way of accomplishing the > whole task? -- You received this message because you are subscribed to the Google Groups "ActiveScaffold : Ruby on Rails plugin" 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/activescaffold?hl=en.
