What's New in Edge Rails: Object Transactions Are Out

Posted by ryan
at 9:41 PM on Monday, March 19, 2007



The days of natively supported object transactions are gone. Object transactions you say? Yeah, that nifty little trick that performs rollbacks not only of the database state, but of the object state as well:

1
2
3
4
Account.transaction(from, to) do
  from.withdraw(100)
  to.deposit(100)
end

If there was a failure within the transaction block above not only would the database have been rolled back, but the internal state of each object argument (from and to in this case) would have as well.

This was previously deprecated, so if you’re still using it, shame on you. All is not lost, however – you can use bitsweat’s object transaction plugin to get this behavior.

tags: ruby, rubyonrails

Comments

Leave a response

  1. Austin ZieglerMarch 19, 2007 @ 10:38 PM

    Glad to hear this… there have been periodic problems with the presence of a copy of Transaction::Simple in the Rails stuff. Transaction::Simple has been upgraded twice since Rails was released and there appear to have been some conflicts between the version required by PDF::Writer and that included with Rails.

  2. JaredMarch 20, 2007 @ 11:02 AM

    I was surprised to see this, I haven’t been getting any deprecation warnings about Object Transactions, and I use them in almost all my controllers.

    I guess I’ll switch over to the plugin, unless there’s a better way to do the following:

    def create
      @personnel = @resource.personnel.build(params[:personnel])
    Personnel.transaction do 
      @personnel.save!
      respond_to do |format|
        flash[:notice] = 'Personnel was successfully created.'
        format.html { redirect_to resource_person_url(@personnel) }
        format.xml  { head :created, :location => resource_person_url(@personnel) }
      end
    end
    rescue ActiveRecord::RecordInvalid => e
      @personnel.valid?
      respond_to do |format|
        format.html { render :action => "new" }
        format.xml  { render :xml => @personnel.errors.to_xml }
      end
    end
  3. Tamer SalamaMarch 20, 2007 @ 11:59 AM

    Late to the party – but why were these taken off of Rails?

  4. ChrisMarch 20, 2007 @ 02:27 PM

    Jared, I don’t think that transaction is actually doing anything. Try this: http://pastie.caboo.se/48280

  5. RobinMarch 22, 2007 @ 12:36 PM

    Jared, what you posted is not an Object Transaction. It’s just a regular transaction block which will remain and work fine. The way to spot the difference is that Object Transactions take objects as parameters:

    Account.transaction (obj1, obj2) do

    versus

    Account.transaction do

  6. John TopleyMarch 23, 2007 @ 12:23 PM

    Re: Jared’s code: ActiveRecord already wraps inserts, updates and deletes in transactions, so the explicit transaction statement is unnecessary.

  7. HarryMarch 25, 2007 @ 01:08 AM

    Hi. I don’t know if this applies to everyone; I might just have buggy code. However, in order to get object_transactions working I also had to install the transactions-simple gem. I don’t think this is mentioned in the object_transactions plugin README.

  8. BrandonDecember 17, 2007 @ 04:58 PM

    is no one going to answer the question of WHY these were taken out!?

  9. Ryan DaigleDecember 19, 2007 @ 07:16 AM

    Hey Brandon,

    Contrary to popular belief, this is not a Rails support forum (nor do I speak with any authority as to the intents or motives of the Rails-core team). I would make a post to the mailing list if you really want to know why this feature was removed.

    Sorry!

  10. YodaYidDecember 20, 2007 @ 11:12 AM

    From the plugin’s website: “It was converted into a plugin since it’s not commonly needed.” http://code.bitsweat.net/svn/object_transactions/README