I'm trying to get a before_update
model callback working where quote_file_date
is automatically timestamped based off whether quote_file
is created, updated or removed. The purpose of this is so that I can track when the file is created and updated.
I'm unsure how to implement ActiveModel::Dirty to get this working properly, but the expected behaviour should be:
quote_file
is created, a quote_file_date
timestamp is created. quote_file
value changes, the quote_file_date
timestamp is updated. quote_file
is removed, the quote_file_date
is set back to nil. Currently, I have:
class Job < ActiveRecord::Base
before_update :quote_file_updated?, on: [:create, :update], if: quote_file.identifier.changed?
private
def quote_file_updated?
self.quote_file_date = Time.now
end
end
And the error I get back from this is:
undefined method `quote_file' for #<Class:0x007fa3bbedfec0>
What is the most elegant way to achieve this using callbacks in the model?
答案是将before_update
回调更改为:
before_update :quote_file_updated?, on: [:create, :update], if: ->(r) { r.quote_file_changed? }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.