Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: How to identify after_commit action in observers? (create/update/destroy)

I have an observer and I register an after_commit callback. How can I tell whether it was fired after create or update? I can tell an item was destroyed by asking item.destroyed? but #new_record? doesn't work since the item was saved.

I was going to solve it by adding after_create/after_update and do something like @action = :create inside and check the @action at after_commit, but it seems that the observer instance is a singleton and I might just override a value before it gets to the after_commit. So I solved it in an uglier way, storing the action in a map based on the item.id on after_create/update and checking its value on after_commit. Really ugly.

Is there any other way?

Update

As @tardate said, transaction_include_action? is a good indication, though it's a private method, and in an observer it should be accessed with #send.

class ProductScoreObserver < ActiveRecord::Observer
  observe :product

  def after_commit(product)
    if product.send(:transaction_include_action?, :destroy)
      ...

Unfortunately, the :on option does not work in observers.

Just make sure you test the hell of your observers (look for test_after_commit gem if you use use_transactional_fixtures) so when you upgrade to new Rails version you'll know if it still works.

(Tested on 3.2.9)

Update 2

Instead of Observers I now use ActiveSupport::Concern and after_commit :blah, on: :create works there.

like image 877
elado Avatar asked Aug 30 '11 04:08

elado


4 Answers

I think transaction_include_action? is what you are after. It gives a reliable indication of the specific transaction in process (verified in 3.0.8).

Formally, it determines if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.

class Item < ActiveRecord::Base
  after_commit lambda {    
    Rails.logger.info "transaction_include_action?(:create): #{transaction_include_action?(:create)}"
    Rails.logger.info "transaction_include_action?(:destroy): #{transaction_include_action?(:destroy)}"
    Rails.logger.info "transaction_include_action?(:update): #{transaction_include_action?(:update)}"
  }
end

Also of interest may be transaction_record_state which can be used to determine if a record was created or destroyed in a transaction. State should be one of :new_record or :destroyed.

Update for Rails 4

For those seeking to solve the problem in Rails 4, this method is now deprecated, you should use transaction_include_any_action? which accepts an array of actions.

Usage Example:

transaction_include_any_action?([:create])
like image 65
tardate Avatar answered Oct 19 '22 14:10

tardate


I've learned today that you can do something like this:

after_commit :do_something, :on => :create

after_commit :do_something, :on => :update

Where do_something is the callback method you want to call on certain actions.

If you want to call the same callback for update and create, but not destroy, you can also use: after_commit :do_something, :if => :persisted?

It's really not documented well and I had a hard time Googling it. Luckily, I know a few brilliant people. Hope it helps!

like image 28
Jure Triglav Avatar answered Oct 19 '22 12:10

Jure Triglav


You can solve by using two techniques.

  • The approach suggested by @nathanvda i.e. checking the created_at and updated_at. If they are same, the record is newly created, else its an update.

  • By using virtual attributes in the model. Steps are:

    • Add a field in the model with the code attr_accessor newly_created
    • Update the same in the before_create and before_update callbacks as

      def before_create (record)
          record.newly_created = true
      end
      
      def before_update (record)
          record.newly_created = false
      end
      
like image 7
leenasn Avatar answered Oct 19 '22 14:10

leenasn


Based on leenasn idea, I created some modules that makes it possible to use after_commit_on_updateand after_commit_on_create callbacks: https://gist.github.com/2392664

Usage:

class User < ActiveRecord::Base
  include AfterCommitCallbacks
  after_commit_on_create :foo

  def foo
    puts "foo"
  end
end

class UserObserver < ActiveRecord::Observer
  def after_commit_on_create(user)
    puts "foo"
  end
end
like image 4
lacco Avatar answered Oct 19 '22 12:10

lacco