Reputation: 2723
I have an after_create filter that leads to the following definition
def create_slug
candidate = [self.make, self.model, self.year]
self.slug = candidate.join('=').parameterize
self.save!
end
I found out the SQL statement would take 6.4ms compare to usual 0.1 - 0.5ms
I tried changed it to self.update_attributes
def create_slug
candidate = [self.make, self.model, self.year]
self.update_attributes(slug: candidate.join('=').parameterize)
end
the SQL statement is 4.7ms.
I wonder if there is any difference in using the two methods.
Upvotes: 0
Views: 751
Reputation: 115541
save!
is meant to raise on failure, not update_attributes
which would return false
. Thats the only difference I see, let aside the method signature
If you look at the internal its more obvious:
# File activerecord/lib/active_record/persistence.rb, line 246
def update(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
assign_attributes(attributes)
save
end
end
Upvotes: 1