23

I'm trying to set a boolean field to false based on the value of another boolean field. I tried the following with an ActiveRecord model:

  before_save :reconcile_xvent

  def reconcile_xvent
    self.xvent_hood = false if !self.xvent_plenum?
  end

But this doesn't work. Now, many of my unit tests fail with:

ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved

How can I set xvent_hood to be false if xvent_plenum is false?

Update

Here's what works (some of which comes from the comments/answers below):

before_validation :reconcile_xvent

def reconcile_xvent
  if self.xvent_hood?
    self.xvent_hood = false unless xvent_plenum?
  end
end

I couldn't figure out to make it work without the "if self.xvent_hood?" part....

3
  • If xvent_hood needs to be equal xvent_plenum, maybe you should just do a xvent_hood = xvent_plenum.
    – MurifoX
    Aug 2, 2012 at 17:54
  • But they won't always be equal. xvent_plenum should be able to be true, and xvent_hood false.
    – croceldon
    Aug 2, 2012 at 17:56
  • 1
    Try using unless then. self.xvend_hood = false unless self.xvent_plenum.
    – MurifoX
    Aug 2, 2012 at 17:58

2 Answers 2

26

before_save is only called after validation has passed. What you need to do is move reconcile_xvent up to before_validation rather than before_save

If you keep that method in before_save what will happen is that it thinks that xvent_hood is null, if you have a validation that checks for nullity of xvent_hood it will fail before the before_save gets called. Which probably explains why you got RecordNotSaved error.

Another thing to keep in mind is that if you have a boolean property, you also can't use validate_presence_of. See http://alexanderwong.me/post/16084280769/rails-validate-presence-of-boolean-and-arrays-mongoid

3
  • This seems to work, but why does before_save think xvent_hood is null?
    – croceldon
    Aug 2, 2012 at 18:08
  • it's not that before_save thinks xvent_hood is null, I believe what happend is that it failed to pass validation before before_save is even called in the first place. (Assuming if you have the proper validations in place that is, which is the reason why your records are not saving)
    – Ken Li
    Aug 2, 2012 at 18:09
  • would be cool to see a link to the before_save documentation here.
    – botbot
    May 15, 2015 at 8:44
18

I just hit this issue also, the problem with the first block of code is that you're assigning the xvent_hood value to false which is then returned by your before_save method.

as per

Canceling callbacks

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.

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

3
  • This is the correct answer BUT how do you resolve the issue? How do you assign a boolean to false in a before_save callback?
    – csi
    Aug 2, 2015 at 22:21
  • 3
    To assign a boolean to false in your before_save ensure you explicitly return true at the end of your callback. For example: def before_save(record) my_false_bool = false return true end
    – lyallward
    Aug 4, 2015 at 1:16
  • That is the first time i've seen ruby behave in an obnoxious manner due to it omitting regular keywords like return. OMFG that is frusterating. Jul 26, 2017 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.