문제

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 ?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top