Rails 3: how to update only 1 attribute?
How can I update only 1 attribute in the database WITHOUT having to insert the validations?
e.g:
I have password as presence = true. When I want to edit the database, I have to pass the 开发者_JAVA技巧password. I want to edit 1 field without passing the password.
I've tried update_attribute, merge, but none works.
Thanks. Donald
Edit: Even with the validation, which works on the console, it still puts the password there.
Here's the validation:
def password_validation_required?
encrypted_password.blank? || !@password.blank?
end
And when I submit the form without the password field, I get this on the console: (it puts blank on the encrypted_password field)
SQL (0.3ms) UPDATE "wsps" SET "about" = 'gfg', "encrypted_password" = 'fcf538f9a588befec4ee2567754a42f05b3cd75f24919d49530426786491c3e1', "updated_at" = '2010-11-30 23:56:45.594168' WHERE ("wsps"."id" = 4)
Maybe my controller isn't correct? i have this:
if @wsp.update_attributes(params[:wsp])
My form:
<%= form_for(@wsp, :html => { :multipart => true } ) do |f| %>
thanks
Validations and callbacks are designed to be pretty hard to ignore. You could do something like:
Model.update_all({:my_attribute => 'x'}, {:id => id})
http://rubydoc.info/docs/rails/3.0.0/ActiveRecord/Relation:update_all
精彩评论