Detecting tranactions in Ruby on Rails

I have a Ruby on Rails question: 

I need a way to detect if the code is executing under a transaction.

So that I'll be able to enforce save only under transaction in some models. 


Share Send to a friend Watch Report
 
 

5 Posted Answers
Order by

 
4 helpful answers

What I'm trying to do is to make sure certain models are only ever saved inside a transaction.

So I wat to write a macro method such that I can do:

class Foo < ActiveRecrod::Base

  require_transaction

 end

And it will throw an exception if in before_save it detects that there's no transaction going. i.e.

@foo.save! # should fail

AnyOtherModelOrFoo.transaction do

  @foo.save! # should work

end

 

 Next stage will be detecting if the model is locked. i.e. if it was selected with :lock => true. But this is step 2 :-)

 

Posted 2006-09-09T16:32:11Z
 
4 helpful answers

That's not it. the code you linked is just a quick hack intended  to verify which transaction method gets called by setting a trace function and printing all the data on function calls when name contains word transaction and then running some ActiveRecord transaction code. The result: it's a class method


ActiveRecord::Transactions::ClassMethods#transaction
Posted 2006-09-30T00:11:27Z
 

Okay … I completely misread that code. Sorry about that.

Looking at the ActiveRecord transaction docs I wonder if you couldn't check to see that the thread had fired start_db_transaction, as it appears to do so when the transaction is started. However the Transaction::Simple module has a method transaction_open? that looks like it could verify a transaction was currently open, using named transactions.

I must admit I don't do much with RoR, so if the above doesn't help I hope hornbeck can provide some insight. :)

Posted 2006-09-30T02:10:07Z
rholliday was invited by Yedda to answer this question.

 
8 helpful answers
Agile like Ninja 42squared

Pacman,

 

what form of transaction are you trying to detect?  Most items that will execute within a rails program can be monitored but the best bet would be to write test for the method/transaction so that you can create strick rules for what is going on.  I would like to know a little more about what it is that you are trying to do so that I can give a little more in depth answer. 

Posted 2006-09-09T15:47:39Z
hornbeck was invited by Yedda to answer this question.

 

It seems like there isn't really a built-in way to do this. I hear a lot about how RoR isn't strong on transactions. However, this looks like kind of a jury-rigged workaround:

if id.to_s =~ /transaction/ then
# inside a transaction
end

 I found this here.

Posted 2006-09-29T17:50:50Z
rholliday was invited by Yedda to answer this question.

Sign in to participate

Got an answer for pacman? Would you like to comment on the posted answers, or vote for the one which you think is the best?

Sign up for a free account, or sign in (if you're already a member).

Explore Related Questions

Other people asked questions on similar topics, check out the answers they received:


Q:

Using Ruby on Rails to build large sites?

Can you build a site using Ruby on Rails that can handle the traffic levels of Myspace?
Submitted by sudonim   3 years ago.
  • viewed 3265 times
Last answer posted 2 years ago by Kuchmuch


Q:

Using Ruby for internal CRM app

I am about to start developing a new internal web application for the company I work for. It will be a lightweight CRM app ...
Submitted by Marti   3 years ago.
  • viewed 2423 times
Last answer posted 3 years ago by david_terrar


Q:

Rails Cascading Includes

Why can't I do cascading includes in ruby on rails: Tree.find(:first, :include => {:children => {:children => :children}}) I get ...
Submitted by richcollins   3 years ago.
  • viewed 482 times
Last answer posted 3 years ago by richcollins



» More...

Feed - Subscribe to changes to this Q&A Blog
ADVERTISEMENT
ADVERTISEMENT
AOL Autos Q&A is powered by Yedda an AOL Company
Copyright © 2006-2010, Yedda Inc. and respective copyright owners