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.
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.
The search for people who can answer your question continues for as long as needed - until you find the answer you were looking for.
When an answer is posted by someone who was invited (byYedda or by yourself) to answer your question, their answer is marked with a yellow "invited by Yedda".
To be invited to answer other people's questions in your areas of knowledge and interest, be sure to list your favorite topics:
» My Settings My Topics.
Of course, the more helpful your answers are, the more likely you are to be invited to future questions...
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
Next stage will be detecting if the model is locked. i.e. if it was selected with :lock => true. But this is step 2 :-)
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:
I found this here.
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
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. :)
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).
Other people asked questions on similar topics, check out the answers they received:
Other people asked questions on various topics, and are still waiting for answer. Would be great if you can take a sec and answer them