I’m trying to avoid having my model set the updated_at attribute when it’s saved without any changes being made. Through the debugging process I’ve noticed that the ‘changed’ array lists an attribute which I know wasn’t actually changed from within the form. That attribute does however have a reader method. That method gives a result which is different from its current state. Is there a way of manually clearing the ‘changed’ flag on that attribute within the reader method?
Wine.rb
before_save :check_for_changes
def blends
if read_attribute(:blends).nil?
"100% #{ name.split(" ").map { |w| varietals.include?(w.downcase) ? w : nil }.compact.join(" ") }"
else
read_attribute(:blends)
end
end
def check_for_changes
return changed?
end
Basically the ‘blends’ method shoots out “100% [varietal name]” if there aren’t any blend %’s listed. This apparently interferes with the ‘changed’ flag…
I avoid overriding field readers because that keeps me from accessing values stored in the database directly (i.e. without having to call read_attribute) for other logic. That’s troublesome when you need to check if there is actually a value stored in the database or encounter situations when there is a value stored but it is also in a format of “100% some-varietal”, as I think you would if you have a form field for this.
I usually add in something like this instead: