Reputation: 2343
I am migrating a rails 5.1 app to rails 5.2.1. In my models I am using callbacks to create an activity log after a model was created or updated. Unfortunately todo.name
and todo.name_was
is always the same - the current value. This applies to every attribute and every model. Also changed?
returns false.
Am I missing something?
I appreciate your help a lot!
Upvotes: 5
Views: 7485
Reputation: 3175
You won't get attribute_was
in after_create/update callback as the records has been changed in DB at this point.
You can make use of previous_changes
in after_create/update
callbacks.
Here is an example below.
Consider, User Model:
class User < ApplicationRecord
before_update :check_for_changes
after_update :check_for_previous_changes
private def check_for_changes
puts changes # => {"name"=>["Nimish Gupta", "Nimish Mahajan"], "updated_at"=>[Tue, 20 Nov 2018 00:02:14 PST -08:00, Tue, 20 Nov 2018 00:06:15 PST -08:00]}
puts previous_changes # => {} At this point this will be empty beacuse changes are not made to DB yet
puts name_was # => "Nimish Gupta" i.e the original name
puts name # => "Nimish Mahajan" i.e the new name which will be going to save in DB
end
private def check_for_previous_changes
puts changes # => {}
# Please note `changes` would be empty now because record have been saved in DB now
# but you can make use of previous_changes method to know what change has occurred.
puts previous_changes # => {"name"=>["Nimish Gupta", "Nimish Mahajan"], "updated_at"=>[Tue, 20 Nov 2018 00:06:15 PST -08:00, Tue, 20 Nov 2018 00:08:07 PST -08:00]}
puts name_was # => "Nimish Mahajan" i.e the new name which have been saved in DB
puts name # => "Nimish Mahajan" i.e the new name which have been saved in DB
# to get the previous name if after_create/update callback, Please use.
puts previous_changes[:name][0]
end
end
u = User.first # => #<User id: 1, name: "Nimish Gupta">
u.update(name: 'Nimish Mahajan') # => this will fire both before_update and after_update callbacks.
Hope this will help you.
You can also take a look at this answer for more information
Upvotes: 13
Reputation: 1892
I think you are using changed?
on a persisted objet, attribute_was
, changed?
only works when object is dirty see for more details https://api.rubyonrails.org/classes/ActiveModel/Dirty.html
class Todo < ApplicationRecord
end
todo = Todo.new
todo.changed? # => false
todo.persisted? # => false
If you save the object in DB the object becomes persisted(no more dirty data present)
todo = Todo.new(name: 'Test')
todo.name_was # => nil
todo.name_changed? #=> true
todo.save
todo.persisted? # => true
todo.name_was # => Test
todo.name_changed? #=> false
Upvotes: 4