Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: update_attributes not updating all attributes

I have a simple model called Discussion which has a boolean column called resolved.

In my form, I have the following code

<%= form_for(@discussion) do |d| %>
...
<%= d.check_box :resolved %>
<% end %>

And in my controller, I have the following:

def update
  @discussion = Discussion.find(params[:id])
  if @discussion.update_attributes(params[:discussion])
    etc...
  end
end

When I submit the form, I can see that the parameters are being sent to the server...

Parameters: {"utf8"=>"✓", "authenticity_token"=>"AsGsRHwiVva/+kTrBs0IjLeZwj1ZmXBuKZr9Pg/N6Xk=", "discussion"=>{"shortdesc"=>"Talk about something.", "content"=>"Try to update check box.", "resolved"=>"1"}, "commit"=>"Update Discussion", "id"=>"1"}

But the query doesn't include anything about updating that field.

AREL (14.9ms)  UPDATE "discussions" SET "content" = 'Try to update check box.', "updated_at" = '2011-07-18 17:53:50.783176' WHERE "discussions"."id" = 1

Any idea on what I'm missing?

like image 887
Kevin Thompson Avatar asked Nov 30 '22 08:11

Kevin Thompson


1 Answers

There are 4 reasons why this could be happening:

  1. resolved is already set to true in the database.
  2. You defined the resolved= method in your model and it no longer sets the attribute.
  3. You have attr_protected :resolved.
  4. You have attr_accessible but do not have :resolved in the list.
like image 84
Samuel Avatar answered Dec 04 '22 01:12

Samuel