Change Attribute X if Attribute Y changes?

时间:2015-06-25 18:44:34

标签: ruby-on-rails activerecord callback

I am familiar with Dirty Objects and using attribute_changed? in a presave callback. I'd like to change one attribute back to a default value when the attribute Y is changed. So in pseudocode: def pre_save self.crawl_mode = false if self.url_changed? end However, changing the url and trying to save fails. It still passes all validations, so I'm not sure where to go from here.

1 个答案:

答案 0 :(得分:1)

self.crawl_mode = false will cause the method to return false which will abort the save. try this: def pre_save self.crawl_mode = false if self.url_changed? true end Ruby methods implicitly return the value of the last assignment (more precisely, last expression)