Reputation: 9491
I have been trying to figure out when exactly each callback for active record is called in rails.
My specific problem is: does a before_save ever not get called. For example if you do a create.
The broader approach is when and where is each called. Also say you modify the existing object in a call back which will you have to re-save in, and can you force a save inside a callback, will that save call the save_callbacks... You see where I am going.
Upvotes: 0
Views: 270
Reputation: 50057
I am not entirely sure what you mean, but if you want to change something inside a callback that would warrant a save, to do it in a before_save
callback, and don't save explicitly, since you know the item is going to be saved next anyway.
In an after_save
you would better not change anything and save, since indeed that will trigger the callbacks again, and result in an endless loop.
It is possible to declare the callbacks conditionally though, and this might solve your problem as well:
before_save :do_something, :unless => some_condition
More examples could be found here.
Anyway, if this is still unclear, or not completely to the point for your case: it is easier to discuss this if you give a more practical example.
Upvotes: 1
Reputation: 20724
Your question is a bit unclear maybe http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html could help
Upvotes: 0