Is there a way to make a before_save conditional?

后端 未结 2 604
误落风尘
误落风尘 2021-02-06 22:40

I am trying to make a before_save in a rails app conditional, but it doesn\'t seem to be working.

before_save method_call_to_run if self.related_model.some_metho         


        
2条回答
  •  终归单人心
    2021-02-06 23:14

    you can use :if

    before_save do_something, :if => Proc.new {|model| model.some_boolean_attr_or_method }
    

    or simply

    before_save do_something, :if => some_condition
    

    EDIT:

    for a quick reference, there's an excellent guide about this:

    http://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks

提交回复
热议问题