Interestingly enough, update_all does not call after_update, so I
just settled with following code for now.
Please let me know if you have smarter solutions.
def after_update
Action.update_all("latest = ''''",
["inquiry_id = ?",
self.inquiry_id])
latest_action = Action.find(:first, :conditions => ["deleted
<> 1
and inquiry_id = ?", self.inquiry_id], :order => "action_date DESC,
id
DESC")
Action.update_all("latest = ''Y''", ["id =
?", latest_action.id]) if
latest_action
end
On Sep 21, 10:34 am, Glenn
<withhaw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi,
>
> I am trying to replace my old database trigger with ActiveRecord
> callbacks in rails.
> I have a model like following:
>
> class Action < ActiveRecord::Base
> belongs_to :inquiry
>
> def after_update
> #clean up all action associated with the inquiry.
> Action.update_all("latest = ''''",
["inquiry_id = ?",
> self.inquiry_id])
> #find the latest action
> latest_action = Action.find(:first, :conditions => ["deleted
<> 1
> and inquiry_id = ?", self.inquiry_id], :order => "action_date
DESC, id
> DESC")
> #mark ''Y'' to the latest action.
> latest_action.update_attribute("latest", "Y") if
latest_action
> end
>
> end
>
> Model "Inquiry" has many "actions", so I want to
maintain the field
> called "latest" to indicate the latest action.
>
> However, this will not work. My after_update call another update, and
> after_update is recursively called. This will end up with getting
> stack overflow.
>
> Are there any way to temporary disable after_update callbacks?
>
> Thanks,
> Glen
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---