2

So this may have been a mistake.

In my model, on my orders table, I have a field named active. Its a boolean value and defaults to true. In a before_create on orders, I'm doing some logic that will set active to false if XYZ occurs.

When XYZ happens though, it seems to stop the record from being saved all together (without providing me any real errors), rather than just the field being set as false.

Any ideas?

2 Answers 2

10

If you set active = false at the end of your before_create filter, the filter will return false and halt the filter execution chain, cause the save not to complete.

If this is the case in your code, just return true at the end of your before_create and you should be fine.

6
  • I'm talking about setting self.active to false - is that different?
    – Elliot
    Aug 26, 2012 at 1:03
  • @Elliot Nope, that still returns false. Aug 26, 2012 at 1:03
  • I dont understand why - if I have active defined as a field in my model, why should it halt the execution chain if I define it as false?
    – Elliot
    Aug 26, 2012 at 1:08
  • It's not the setting of the field that halts, it's the fact that it is the last value in the before_create method.
    – DGM
    Aug 26, 2012 at 1:14
  • 1
    Elliot, 1. any method in ruby returns the last expression executed with an implicit return. If the last expression you execute is "self.active = false", then false will be the value returned. 2. Any validation or callback (before_create, before_update, etc.) that returns false will halt the validation and it wont save.
    – traday
    Aug 26, 2012 at 1:21
0

I have the same problem and I have to insert 1==1 or true at the end of every before_create callback. Looks like something weird in callback definition.

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.