John
John

Reputation: 515

Change Attribute X if Attribute Y changes?

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.

Upvotes: 0

Views: 26

Answers (1)

Adnan
Adnan

Reputation: 2967

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)

Upvotes: 1

Related Questions