Rails: check if the model was really saved in after_save - ruby-on-rails

ActiveRecord use to call after_save callback each time save method is called even if the model was not changed and no insert/update query spawned.
This is the default behaviour actually. And that is ok in most cases.
But some of the after_save callbacks are sensitive to the thing that if the model was actually saved or not.
Is there a way to determine if the model was actually saved in the after_save?
I am running the following test code:
class Stage < ActiveRecord::Base
after_save do
pp changes
end
end
s = Stage.first
s.name = "q1"
s.save!

ActiveRecord use to call after_save
callback each time save method is
called even if the model was not
changed and no insert/update query
spawned.
ActiveRecord executes :after_save callbacks each time the record is successfully saved regardless it was changed.
# record invalid, after_save not triggered
Record.new.save
# record valid, after_save triggered
r = Record.new(:attr => value)
# record valid and not changed, after_save triggered
r.save
What you want to know is if the record is changed, not if the record is saved.
You can easily accomplish this using record.changed?
class Record
after_save :do_something_if_changed
protected
def do_something_if_changed
if changed?
# ...
end
end
end

After a save, check to see if the object saved is a new object
a = ModelName.new
a.id = 1
a.save
a.new_record?
#=> false

For anyone else landing here, latest rails 5 has a model method saved_changes? and a corresponding method saved_changes which shows those changes.
book = Book.first
book.saved_changes?
=> false
book.title = "new"
book.save
book.saved_changes?
=> true
book.saved_changes
=> {"title" => ["old", "new"]},

Related

How to determine changed attributes on after_save callback

I can't catch the changed attributes on after_save callback but I can catch them on after_update callback. I think after_save is just a combination of after_create and after_update. I'd appreciate it if someone give me at least a hint.
class Student < ApplicationRecord
after_save: after_save_callback
def after_save_callback
if username_changed? ### This is always false
# do something
end
end
end
student = Student.create(name: 'John Doe', username: 'abcdef')
student.username = '123456'
student.save
My Rails version is 5.0.7.
You can use saved_change_to_username?
The behavior of attribute_changed? inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after save returned (e.g. the opposite of what it returns now). To maintain the current behavior, use saved_change_to_attribute? instead.

How to stop create in rails 5 without rolling back changes?

I need to check if a similar record exist in database before save, if true then update the existing record without saving, else create a new one
In rails 5:
returning false in a hook method doesn't halt callbacks and "throw :abort" is used instead.
the problem is using "throw :abort" rolls back any changes made in the before_save callback.
what I am trying to do is to check for a similar recored in "before_save" and if a similar record exist I need to update the current record and stop saving the new one.
I used
before_save :check
def check
if (similar record exist..)
update current...
return false <==========
end
true
end
but this is not working any more in Rails 5 so returning false doesn't stop it from saving the new record too.
and I tried
before_save :check
def check
if (exist..)
update current...
throw :abort <========
end
true
end
this stops saving current record to db but it perform "rollback" so the updated recored is missed !!
how can I do that ?
I think this is one possible way. This example if with a Product model looking for same name.
before_create :rollback_if_similar_exists
after_rollback :update_existing_record
def rollback_if_similar_exists
throw :abort if Product.exists? name: self.name
end
def update_existing_record
# do here what you need
puts name
puts "find the existing record"
puts "update data"
end
Here is a slightly different approach you could take:
Instead of using before_save, create your own validation and use assign_attributes instead of update or create since assign_attributes won't actually write to the database. You could then call the valid? function on your record to execute your validations. If you get a duplicate record error from the validation you defined, then have your code handle updating the existing record in the logic of your error handling.
Something like this in your controller:
#record.assign_attributes(my_parameter: params[:my_parameter])
if #record.valid?
#record.save
else
puts #record.errors.messages.inspect
#update your existing record instead.
end
Then in your model:
validate :my_validation
def my_validation
if record_already_exists
return errors.add :base, "Your custom error message here."
end
end
I'd recommend using #find_or_initialize_by or #find_or_create_by to instantiate your instances. Instead of placing record swapping code inside a callback. This means you'll do something like this (example controller create):
class Post < ApplicationController
def create
#post = Post.find_or_initialize_by(title: param[:title])
if #post.update(post_params)
redirect_to #post
else
render :new
end
end
end
Pair this with a validation that doesn't allow you to create double records with similar attributes and you're set.
create_table :posts do |t|
t.string :title, null: false
t.text :body
end
add_index :posts, :title, unique: true
class Post < ApplicationRecord
validates :title, presence: true, uniqueness: true
end
I don't recommend the following code, but you could set the id of your instance to match the record with similar data. However you'll have to bypass persistence (keeps track of new and persistent records) and dirty (keeps track of attribute changes). Otherwise you'll create a new record or update the current id instead of the similar record id:
class Post < ApplicationRecord
before_save :set_similar_id
private
def set_similar_id
similar_record = Post.find_by(title: title)
return unless similar_record
#attributes['id'].instance_variable_set :#value, similar_record.id
#new_record = false
end
end
Keep in mind that only changes are submitted to the database when creating a new record. For new records these are only the attributes of which the attributes are set, attributes with value nil are not submitted and will keep their old value.
For existing records theses are the attributes that are not the same as there older variant and the rule old_value != new_value (not actual variable names) applies.

Update another column if specific column exist on updation

I have a model which have two columns admin_approved and approval_date. Admin update admin_approved by using activeadmin. I want when admin update this column approval_date also update by current_time.
I cant understand how I do this.Which call_back I use.
#app/models/model.rb
class Model < ActiveRecord::Base
before_update 'self.approval_date = Time.now', if: "admin_approved?"
end
This assumes you have admin_approved (bool) and approval_date (datetime) in your table.
The way it works is to use a string to evaluate whether the admin_approved attribute is "true" before update. If it is, it sets the approval_date to the current time.
Use after_save callback inside your model.
It would be something like this:
after_save do
if admin_approved_changed?
self.approval_date = Time.now
save!
end
end
Or change the condition as you like!
You could set the approval_date before your model instance will be saved. So you save a database write process instead of usage of after_save where you save your instance and in the after_save callback you would save it again.
class MyModel < ActiveRecord::Base
before_save :set_approval_date
# ... your model code ...
private
def set_approval_date
if admin_approved_changed?
self.approval_date = Time.now
end
end
end
May be in your controller:
my_instance = MyModel.find(params[:id])
my_instance.admin_approved = true
my_instance.save

Tracking model changes in after_commit :on => :create callback

Is there a way to track changes to model on after_commit when a record is created? I have tried using dirty module and was able to track changes when the record was updated, but when record is created changes are not recorded.
You can't use the rails changed? method, as it will always return false. To track changes after the transaction is committed, use the previous_changes method. It will return a hash with attribute name as key. You can can then check if your attribute_name is in the hash:
after_commit :foo
def foo
if previous_changes[attribute_name]
#do your task
end
end

Rails, using the dirty or changed? flag with after_commit

I heard rails has a dirty/change flag. Is it possible to use that in the after_commit callback?
In my user model I have:
after_commit :push_changes
In def push_changes I would like a way to know if the name field changed. Is that possible?
You can use previous_changes in after_commit to access a model's attribute values from before it was saved.
see this post for more info:
after_commit for an attribute
You can do a few things to check...
First and foremost, you can check an individual attribute as such:
user = User.find(1)
user.name_changed? # => false
user.name = "Bob"
user.name_changed? # => true
But, you can also check which attributes have changed in the entire model:
user = User.find(1)
user.changed # => []
user.name = "Bob"
user.age = 42
user.changed # => ['name', 'age']
There's a few more things you can do too - check out http://api.rubyonrails.org/classes/ActiveModel/Dirty.html for details.
Edit:
But, given that this is happening in an after_commit callback, the model has already been saved, meaning knowledge of the changes that occurred before the save are lost. You could try using the before_save callback to pick out the changes yourself, store them somewhere, then access them again when using after_commit.
Since Rails 5.1, in after_commit you should use saved_change_to_attribute?
Ref: Rails 5.1.1 deprecation warning changed_attributes

Resources