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.
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])
check for failures:
user.update_attributes!(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With