Reputation: 6944
Im trying to update an sqlite table row(id, data columns) from rails console. All the models attributes are updating but when updating the id attribute of the model, nothing happens.
irb(main):023:0> r = Person.find(4)
=> #<Person id: 4, data: "threeplusone">
irb(main):024:0> r.update_attributes({:id=>5})
=> true
irb(main):025:0> r.save
=> true
Above prints true but when i inspect the sqlite db id is not updated to 5.
irb(main):026:0> r = Person.find(4)
=> #<Person id: 4, data: "threeplusone">
irb(main):027:0> r.update_attributes({:data=>'TRIplusone'})
=> true
irb(main):028:0> r.save
=> true
Above prints true and the data attribute is updating well.
And my model
class Person < ActiveRecord::Base
attr_accessor :id,:data
attr_accessible :id,:data
end
Thanks for any help.
Upvotes: 1
Views: 1030
Reputation: 124419
Rails won't let you update the id
attribute easily. You could execute the following SQL though, and it should work. (Also, when you use the update_attributes(...)
method, you don't need to call .save
afterward).
sql = "update people set id=5 where id=4"
ActiveRecord::Base.connection.execute(sql)
Upvotes: 4