Alex Rivera | Logout

rails 3 : Do i need to give return true in a before_save callback for an object.save to work?

Asked 2011-01-19T12:20:38.720
27
Class User  
  before_save :set_searchable

  def set_searchable  
    self.searchable = true if self.status == :active  
  end  
end  

>> u = User.last  
>> u.save  
false  

u.save always return false. If i remove the before_save it works also if i give a return true in before_save it works

so do i need to give return statements in before_save ? will ActiveRecord saves an object if the before_save returns false ?

Where can i see a full documentation regarding callbacks and its workflow .

Thanks in advance

Edit
Report

1 Answer

35

From: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last.

So, yes.

answered 2011-01-19T12:25:59.600

Your Answer