Question

I have searched and found the sample:

Account.transaction do 
  paul.deposit(10)
  peter.withdraw(10) 
end

I guess the transaction working as that "when a transaction done, begin to do another transaction". deposit/withdraw is a update SQL command. if i can use select(query) SQL command in that like this sample?

Account.transaction do
  condition = person.where("name='paul' and money>=100'").count  # query SQL command
  if condition>0
      paul.deposit(10)
  end 
end

All sample i found is update SQL transaction sample. can i guess this code work without error value, if there are two transaction ?

Was it helpful?

Solution

To make it work you have to LOCK the SELECT query. In that way you are locking these rows, making use of the SELECT … FOR UPDATE SQL syntax.

condition = person.where("name='paul' and money>=100'").lock(true).count

OTHER TIPS

yes, it will work.

When you pass code inside transaction block , it simply means that if anything fails inside that block, everything will be rollback. It also means that there will no inner or say nested db transaction happen which rails provide it by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top