I have a UserObserver with after_commit
(changed from after_create
to avoid a race condition error that id not found) to update the count. But I know for every create, update is executing the after_commit
code (which I knew would happen). How can I run the after_commit
only on create? I have few solutions but a bit confused. I've tried:
- Using
after_commit :do_something, :on => :create
in the model. - Checking the
created_at
andupdated_at
in the observer; if they're the same, then it's a new record. - Using
user.new_record?
I am confused as I want to use 3rd one, but it's not working and I don't know why. I want to use observers only and not the model. Any ideas on this?