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_method_that_returns_t_or_f?If the 'some_method_that_returns_t_or_f' returns true, I want it to run the method before it saves the object otherwise I just want it to ignore the before_save. 解决方案 you can use :ifbefore_save do_something, :if => Proc.new {|model| model.some_boolean_attr_or_method }or simplybefore_save do_something, :if => some_conditionEDIT:for a quick reference, there's an excellent guide about this:http://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks 这篇关于有没有办法使 before_save 有条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 20:23