Rails 3: rollback for after_create
after_create is a part of the transaction saving the current model. Therefore, if your code crashes or if after_create returns false, it should rollback the current transaction and invalidate the enrollment
saving.
If you want to simulate this, add this to your after_create and see if everything works as expected :
raise Exception.new("CRASH")
Returning false
from after_create
will do nothing.
The whole callback chain is wrapped in a transaction. If any before callback method returns exactly false or raises an exception, the execution chain gets halted and a ROLLBACK is issued; after callbacks can only accomplish that by raising an exception.
Also, you must raise ActiveRecord::Rollback
:
Any exception that is not ActiveRecord::Rollback will be re-raised by Rails after the callback chain is halted. Raising an exception other than ActiveRecord::Rollback may break code that does not expect methods like save and update_attributes (which normally try to return true or false) to raise an exception.
http://guides.rubyonrails.org/active_record_callbacks.html#halting-execution
I do something like this:
after_create do
if condition
errors.add(:attr, 'Blah blah blah.')
raise ActiveRecord::Rollback
end
end
For Rails 3: http://guides.rubyonrails.org/v3.2.13/active_record_validations_callbacks.html#halting-execution