63

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.

paradoja
  • 2,989
  • 2
  • 23
  • 33
elado
  • 7,678
  • 9
  • 45
  • 56
  • Are you trying to know if your record was new or not when the after_commit is fired ? RE-reading your question and the answers, I find it confusing. Could you rephrase it or give us a clear example ? – charlysisto Oct 03 '11 at 12:24
  • Your initial solution does work if you use a single-threaded server. If you're not using one, then switch to one, such as unicorn, that'll solve this issue in a clean way. It makes the programming model so much easier, you'll have less headaches (like this one), and ultimately it's faster. Using +transaction_include_action?+ isn't clean, as it's an unsupported protected rails method not backed by any tests in the rails test suite. The next version might not have that method. – Jim Soho Oct 30 '11 at 22:58
  • @elado I'm confused. The accepted answer (tardate's) doesn't work with observers (as noted by ches's comment). Have you switched to using callbacks instead? Please append an explanation to your question. – Kelvin Jul 05 '12 at 19:29
  • @Kelvin, see my question update on how to make it work with Observers. – elado Dec 10 '12 at 23:14

9 Answers9

61

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])
MhdSyrwan
  • 1,441
  • 3
  • 18
  • 25
tardate
  • 15,050
  • 14
  • 48
  • 49
  • 2
    Awesome. This is the cleanest way to do this I have seen. In other cases I have used the solution put forward by @charlysisto (which does work) but this feels nicer. I will be trying this out. – Andrew Hubbs Oct 04 '11 at 00:25
  • It should be noted that both of these methods are protected, so if you want to call them in an observer you have to call `:send` on the model object with the method in question. – nirvdrum Oct 10 '11 at 19:04
  • 4
    Don't use this with new versions of rails, use the methods below: You can do `after_commit :method_name, on: :create` – Preston Marshall May 08 '12 at 05:07
  • 2
    @chris-finne , @preston-marshall yes you are right. If you are on 3.1/3.2 then `after_commit :do_something, :on => :create` is the way to go. But back in 3.0 it was different. "future-proof rails" is a bit of an oxymoron! – tardate May 08 '12 at 13:27
  • 5
    The original question was referring to an observer. You still cannot make use of `:on => :create` in observers, which are otherwise a very clean way to handle things that are not the responsibility of the model class, like sending notifications. Having to use callbacks defeats the purpose of keeping such logic out of the models. – ches Jun 04 '12 at 20:11
  • 2
    +1. nice to know, but I agree w/ ches that it doesn't work with observers. – Kelvin Jul 05 '12 at 19:35
  • See my question update on how to make it work with Observers. – elado Dec 10 '12 at 23:13
  • 1
    is there a method that will just tell you what the transaction action is? something that will just return :create, :update, or :destroy? – taelor Jun 28 '13 at 18:14
  • 1
    I can confirm this method doesn't exist on rails 4 or 4.1 anymore, instead there is `transaction_include_any_action?` which accepts an array of actions. – John Nov 19 '14 at 13:11
56

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!

Jure Triglav
  • 1,687
  • 15
  • 22
7

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
      
leenasn
  • 1,456
  • 10
  • 16
3

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
lacco
  • 782
  • 1
  • 7
  • 23
  • Why a downvote for this? This code is working without problems in my app, I find it quite useful... – lacco Sep 21 '12 at 07:30
2

Take a look at the test code: https://github.com/rails/rails/blob/master/activerecord/test/cases/transaction_callbacks_test.rb

There you can find:

after_commit(:on => :create)
after_commit(:on => :update)
after_commit(:on => :destroy)

and

after_rollback(:on => :create)
after_rollback(:on => :update)
after_rollback(:on => :destroy)
kenn
  • 3,138
  • 2
  • 26
  • 42
  • Thanks, but it doesn't work. `#` when I do `after_commit(:on => :create){|record| puts "XXX"}` in the observer (It might be working on an activerecord but I use an observer here). – elado Sep 07 '11 at 20:14
  • 2
    It does work in the later versions of rails. You can do `after_commit :method_name, on: :create` – Preston Marshall May 08 '12 at 05:06
0

I'm curious to know why you couldn't move your after_commit logic into after_create and after_update. Is there some important state change that happens between the latter 2 calls and after_commit?

If your create and update handling has some overlapping logic, you could just have the latter 2 methods call a third method, passing in the action:

# Tip: on ruby 1.9 you can use __callee__ to get the current method name, so you don't have to hardcode :create and :update.
class WidgetObserver < ActiveRecord::Observer
  def after_create(rec)
    # create-specific logic here...
    handler(rec, :create)
    # create-specific logic here...
  end
  def after_update(rec)
    # update-specific logic here...
    handler(rec, :update)
    # update-specific logic here...
  end

  private
  def handler(rec, action)
    # overlapping logic
  end
end

If you still rather use after_commit, you can use thread variables. This won't leak memory as long as dead threads are allowed to be garbage-collected.

class WidgetObserver < ActiveRecord::Observer
  def after_create(rec)
    warn "observer: after_create"
    Thread.current[:widget_observer_action] = :create
  end

  def after_update(rec)
    warn "observer: after_update"
    Thread.current[:widget_observer_action] = :update
  end

  # this is needed because after_commit also runs for destroy's.
  def after_destroy(rec)
    warn "observer: after_destroy"
    Thread.current[:widget_observer_action] = :destroy
  end

  def after_commit(rec)
    action = Thread.current[:widget_observer_action]
    warn "observer: after_commit: #{action}"
  ensure
    Thread.current[:widget_observer_action] = nil
  end

  # isn't strictly necessary, but it's good practice to keep the variable in a proper state.
  def after_rollback(rec)
    Thread.current[:widget_observer_action] = nil
  end
end
Kelvin
  • 17,790
  • 2
  • 54
  • 62
  • 2
    Performance and DB locking. If I used after_create/destroy it would make the wrapping transaction longer, with things that I didn't need transactions for. – elado Jul 05 '12 at 23:07
0

I use the following code to determine whether it is a new record or not:

previous_changes[:id] && previous_changes[:id][0].nil?

It based on idea that a new record has default id equal to nil and then changes it on save. Of course id changing is not a common case, so in most cases the second condition can be omitted.

hwo411
  • 11
-1

This is similar to your 1st approach but it only uses one method (before_save or before_validate to really be safe) and I don't see why this would override any value

class ItemObserver
  def before_validation(item) # or before_save
    @new_record = item.new_record?
  end

  def after_commit(item)
    @new_record ? do_this : do_that
  end
end

Update

This solution doesn't work because as stated by @eleano, ItemObserver is a Singleton, it has only one instance. So if 2 Item are saved at the same time @new_record could take its value from item_1 while after_commit is triggered by item_2. To overcome this problem there should be an item.id checking/mapping to "post-synchornize" the 2 callback methods : hackish.

charlysisto
  • 3,710
  • 15
  • 27
  • That would not work because the instance of the observer is a singleton. Meaning, @ variables are shared for all records. If multiple records are handled by the same observer, the values won't be correct. That's why I created an map of IDs and action. – elado Oct 09 '11 at 17:22
  • yup I understand what you meant in the question now that I can see it. Updated the answer accordingly. You learn by your mistakes... – charlysisto Oct 10 '11 at 07:27
-4

You can change your event hook from after_commit to after_save, to capture all create and update events. You can then use:

id_changed?

...helper in the observer. This will be true on create and false on an update.

Winfield
  • 18,155
  • 3
  • 49
  • 65
  • 2
    It's true in `after_create` but false in `after_commit` (for both create and update). – elado Aug 30 '11 at 04:41
  • Updated. Use the after_save event to catch both create/update and be able to distinguish between the two. – Winfield Aug 30 '11 at 13:06
  • 1
    But `after_save` is inside the transaction. I need to execute code outside of it, hence, the use of `after_commit`. – elado Aug 30 '11 at 16:33