How can I read the value that a controller has set in a before_save callback?
Example:
I have a model with a url
field. Before saving, I want to check if the url was changed. If so, do some stuff with both the new and old url.
Is that possible?
Try something like this:
before_save { |m| if m.url_changed? ... }
Also see the docs on ActiveModel::Dirty
better way
before :todo, if: :first_name_or_last_name_changed?
and your todo
method
def first_name_or_last_name_changed?
first_name_changed? || last_name_changed?
end
If it has changed it should be in the params
hash. If it hasn't changed it shouldn't be in there. Therefore, you can put this custom handling into the controller, or in a method on the model that does this.
If you really want to access it inbefore_save
check the documentation on ActiveRecord callbacks, and you will see how to access the before and after values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With