Reputation: 1014
is there any difference in callback adding conditions by using self.attribute_changed? or attribute_changed?
class PlayEvent < ActiveRecord::Base
...
after_save :update_next_event_at!, if: 'self.event_at_changed?'
after_save :update_next_event_at!, if: 'event_at_changed?'
...
end
which one is preferred to use?
Upvotes: 0
Views: 105
Reputation: 36860
There's no difference, the self.
isn't needed.
The only time you need self.
is on assignment. If you have a model attribute comment
and a method like...
def update_comment
comment = "this is the new comment"
end
It may not be what you're expecting, as the assignment in this case creates a variable comment
which is local to the method. Rubocop identifies this as a useless assignment as the comment
variable is not subsequently used anywhere.
However...
def update_comment
self.comment = "this is the new comment"
end
Will correctly change the record attribute.
Upvotes: 1