I have a Printer model to which I added a master boolean column. A printer belongs_to :restaurant. The master flag indicated whether this printer is the primary printer of this restaurant.
I have a method in the Printer model:
def set_master_on_create
if self.restaurant.printers.empty?
self[:master] = true
else
self[:master] = false
end
end
and set this callback: before_create :set_master_on_create
If I run this method as a before_create then creating a new printer will fail. If I change the callback to after_validation creating the new object will work flawlessly, even with the :on => :create option.
Does Rails freeze the object so i can no longer change values after a certain point which will cause the Rollback I get? Any help appreciated.
I am on Rails 3.2.2 and sqlite in dev mode.
i think the problem is that a before_create method returning false will prevent your object from being saved. try adding a
return trueand i guess you set the master attribute like this:
self.master = true