Is there some quick way to find out if any of a model's fields or any fields of it's nested models (a.k.a. associations) changed?
相关问题
- Eager-loading association count with Arel (Rails 3
-
undefined method `type_cast' for #
- Rails how to handle error and exceptions in model
- Is there a way to remove the id column in a subseq
- Scope on each last elements of a has_many relation
相关文章
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Rspec controller error expecting <“index”> but
- Factory_girl has_one relation with validates_prese
- Rails: Twitter Bootstrap Buttons when visited get
- is there a “rails” way to redirect if mobile brows
- Got ActiveRecord::AssociationTypeMismatch on model
- superclass mismatch for class CommentsController (
with 5.1 + the following works for me:
saved_changes?
will let you know if your object has changed:saved_changes
will let you know which fields changed, the before value and the after value for each field:I know this is old question but recently came across same situation.
You can get all changes for your nested model using
previous_changes
method even after saving the object.This will list all changes in the nested model along with old and new value.
You can determine if an object has changed but not yet been saved by calling:
You can also determine if individual attributes have changed (say you have a
first_name
attribute):As far as nested models go, I think you'd have to call
changed?
on them each individually.To see if a ActiveRecord object has changed, you can call:
To see if a specific attribute was changed, you can do:
where attr is the attribute you want to check.
To check for if a nested model changed, you can just do: