Rails: How to use before_save to change a field value based on another field?

Viewed 52626

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....

2 Answers
Related