I've added a before_create filter to one of my Rails ActiveRecord models and inside that filter I'm doing some database updates.
Sometimes I return false from the filter to prevent the creation of the target model, but that is causing all the other database changes I made (while inside the filter) to get rolled back.
How can I prevent that?
Update #1: Here's some pseudo code explaining my problem:
class Widget < ActiveRecord::Base
before_create :update_instead
def update_instead
if some_condition?
update_some_record_in_same_model # this is getting rolled back
return false # don't create a new record
else
return true # let it continue
end
end
end
Update #2: Some good answers below but each had its shortcomings. I ended up overridding the create method like so:
def create
super unless update_instead? # yes I reversed the return values from above
end