On Mar 30, 11:37 am, Alexey Muranov <li...@ruby-forum.com> wrote:
> Robert, it does not seem like i was changing attributes in-place.
>
> Here is what is going on, i do not understand how it can be a desired
> behavior:
>
The core isssue is that changing id in activerecord is meaningless:
every update statement is of the form (simplifying slightly)
update blah set ... where id = #{self.id}
So changing self.id will either update no rows or update some existing
object.
The second thing coming into play is dirty attributes: activerecord
only includes changed attribute in the update clause, so if it thinks
that no attributes have changed since the last save it won't do
anything.
Why are you changing the id of an existing record?
Fred
> I created a test application:
>
> $ rails new test_app
> $ cd test_app
> $ rails generate model Person name:string age:integer
> $ rake db:migrate
>
> In console:
>
> > p = Person.create(:name=>"Johny", :age=>30)
> > p.id # => 1
> > p.id = 2 # => 2
> > p.save # => true
>
> No change in the database!> p.id # => 2
> > p.name = "Jim" # => "Jim"
> > p.save # => true
>
> No change in the database> p.name # => "Jim"
> > p.id = 1 # => 1
> > p.save # => true
>
> No change in the database> p.age = 31 # => 31
> > p.save # => true
>
> In the database, the age became 31, but the name stayed "Johny"!> p.name # => "Jim"
> > p.name.object_id # => 2155015240
> > p.name = "Jim" # => "Jim"
> > p.name.object_id # => 2154887680
> > p.save # => true
>
> No change in the database!> p.name = "Jimmy" # => "Jimmy"
> > p.name.object_id # => 2154859980
> > p.save # => true
>
> Finally, the name changed in the database!
>
> Alexey.
>
> --
> Posted viahttp://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment