Forum: Ruby on Rails strange behaviour in ActiveRecord transaction

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C50ef69869149a93d6487a7ab191f208?d=identicon&s=25 Sandworth Meb (farmerss)
on 2009-05-28 11:17
Hi,
I'm using an ActiveRecord transaction in a Rails app,
and one of the changes made during the transaction is reverted
as the transaction finishes, whilst the other changes are preserved.
The code is like:

------------------------
def swap(other)

  self_number=self.number    #==1
  other_number=other.number  #==2

  ActiveRecordModel.transaction do

    version=Version.find_by_kind(self.kind)
    #version.number==30

    self.number=other_number
    other.number=self_number
    self.save
    other.save

    version.number+=1
    version.save

    #self.number==2
    #other.number==1
    #version.number==31
  end

  #self.number==1    ***Reverted!
  #other.number==1
  #version.number=31

end
------------------------

No exception is being raised by the transaction. Also, I've tried
reversing the order of the assignment and saving on self and other, but
it's still just self.number that reverts. Any ideas?
C50ef69869149a93d6487a7ab191f208?d=identicon&s=25 Sandworth Meb (farmerss)
on 2009-05-28 11:21
it's Rails 2.3.2 on Windows (Vista).
C50ef69869149a93d6487a7ab191f208?d=identicon&s=25 Sandworth Meb (farmerss)
on 2009-05-28 11:22
and MySQL 5.0
A59b0daee5e5570e07a9f976721017e7?d=identicon&s=25 mike (Guest)
on 2009-05-29 09:11
(Received via mailing list)
Perhaps some validation fails on self. Try save! To raise an exception

2009/5/28, Sandworth Meb <rails-mailing-list@andreas-s.net>:
>
> and MySQL 5.0
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>

--
Von meinen Mobilger├Ąt aus gesendet
53a20bb08c1df12985f8d8e01863f246?d=identicon&s=25 sandworth (Guest)
on 2009-05-30 07:10
(Received via mailing list)
Solved.
This was not an issue with ActiveRecord/Rails.
(I had to use "self.number=" instead of "number=". Using "number="
wasn't actually calling the function "number=",
but I presume was creating a local variable "number" instead.)
53a20bb08c1df12985f8d8e01863f246?d=identicon&s=25 sandworth (Guest)
on 2009-05-30 07:11
(Received via mailing list)
I just noticed that in the code sample I submitted, I actually
wrote "self.number=". In my original code I had "number=".
This topic is locked and can not be replied to.