Sunday 8 October 2017

Why rails5 update_attributes doesn’t but Update method does?

Rails version:
Rails 5.1.1
Ruby Version:ruby-2.4.0 [ x86_64 ]
Local Server:Puma starting in single mode…
* Version 3.9.1 (ruby 2.4.0-p0), codename: Private Caller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
I have a existing model called User and later I added the attribute last_name.
Then trying to update the newly created field(last_name) with other fields by rails method called

 User.update_attributes(first_name: ‘praaveen’, last_name:’vr’)
For update_attributes
This updates only first_name field and leave the last_name attribute.
rails log:
UPDATE “users” SET “updated_at” = $1, “first_name” = $2 WHERE “users”.”id” = $3 [[“updated_at”, “2017–09–20 14:19:26.174311”], [“first_name”, “praaveen”], [“id”, 156]]
Then tried with
User.update(first_name: ‘praaveen’, last_name:’vr’)
 User.update_columns(first_name: ‘praaveen’, last_name:’vr’)
These methods updates first_name and last_name as expected.
rails log:
 UPDATE “users” SET “updated_at” = $1, “first_name” = $2, “last_name” = $3 WHERE “users”.”id” = $4 [[“updated_at”, “2017–09–20 14:15:23.623292”], [“first_name”, “praaveen”], [“last_name”, “vr”], [“id”, 156]]
Any idea what’s going?

No comments:

Post a Comment