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

前端 未结 2 2176
孤独总比滥情好
孤独总比滥情好 2021-02-18 18:47

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 :reconc         


        
2条回答
  •  长情又很酷
    2021-02-18 19:34

    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

提交回复
热议问题