Reputation: 1694
In my Rails project I am using Postgresql.
User.find_by_sql("update users set address = '#{params[:address]}' where id = #{current_user.id}")
This code is working fine, but the equivalent activerecord is not working :
user = User.find(current_user.id)
user.update_attributes(:address => params[:address])
it always shows Begin; Rollback
in log.
Can you please explain why is this happening? Is there any issue related to Postgres?
I am using Rails 3.2
EDIT
I found out where i was wrong... a validation fail was causing this. The solution is :
user.update_column(:address,params[:address])
This will bypass validation. Thank you guys for help.
Upvotes: 2
Views: 4647
Reputation: 47532
update_attributes
updates the record if all the validations pass, so one of your validation must have failed, use user.errors
to find out the errors:
user = User.find(current_user.id)
user.update_attributes(:address => params[:address])
puts user.errors
In order to save a single column, use update_attribute
.
user.update_attribute(:address, params[:address])
Upvotes: 5
Reputation: 1831
If you're using attr_accessible
in your model but not including :address, update_attributes will appear to work but the record won't save.
Upvotes: 2