问题
This follows on from my previous request which I answered. But my next issue which I cannot understand is why when my parameters report one record do I get a message that googling/SO searching suggest I need to use an update_all.
has_many/belongs_to build association - why is insert statement for parameter blank?
My update controller method is as follows:
def update
@role = Role.find(params[:id])
logger.debug "@role: id[#{params[:id]}], #{@role}, #{@role.inspect}, #{@role.to_s}"
@permission = @role.permissions(params[:permission])
logger.debug "@permission: #{@permission.inspect}"
respond_to do |format|
if @role.update_attributes(params[:role]) && @permission.update_attributes(params[:permission])
format.html { redirect_to @role, notice: 'Role was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @role.errors, status: :unprocessable_entity }
end
end
end
I get the following error message:
NoMethodError in RolesController#update
undefined method `update_attributes' for #<ActiveRecord::Relation:0x007fb2116b5178>
Rails.root: /Users/railsdev/Development/railsprojects/Genie/BitBucket-Repos/Genie-v3-3
Application Trace | Framework Trace | Full Trace
app/controllers/roles_controller.rb:75:in `block in update'
app/controllers/roles_controller.rb:74:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"3uZ47itmgzUSs83Iq9+2saJn6Y9+Y9tlSDgaRskh9pw=",
"role"=>{"description"=>"T1"},
"permission"=>{"subject_class"=>"User"},
"commit"=>"Update Role",
"id"=>"55"}
Googling and SO searches reveal that I may need to use [update_all
] instead. But why when I only have a single record is this the case?
Here is my console output.
Started PUT "/roles/55" for 127.0.0.1 at 2012-10-01 22:12:16 +0100
Processing by RolesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"3uZ47itmgzUSs83Iq9+2saJn6Y9+Y9tlSDgaRskh9pw=", "role"=>{"description"=>"T1"}, "permission"=>{"subject_class"=>"User"}, "commit"=>"Update Role", "id"=>"55"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'gn668LQGNlLl6HiwPf8DRQ' LIMIT 1
Role Load (0.1ms) SELECT "roles".* FROM "roles" INNER JOIN "assignments" ON "roles"."id" = "assignments"."role_id" WHERE "assignments"."user_id" = 13
Role Load (0.1ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = ? LIMIT 1 [["id", "55"]]
@role: id[55], #<Role:0x007fb2137e3200>, #<Role id: 55, description: "T1", created_at: "2012-10-01 21:11:43", updated_at: "2012-10-01 21:11:43">, #<Role:0x007fb2137e3200>
Permission Load (0.1ms) SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 55
@permission: [#<Permission id: 32, role_id: 55, subject_class: "Diagnosis", action: nil, subject_id: nil, created_at: "2012-10-01 21:11:43", updated_at: "2012-10-01 21:11:43">]
Permission Load (0.1ms) SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 55
Completed 500 Internal Server Error in 5ms
NoMethodError (undefined method `update_attributes' for #<ActiveRecord::Relation:0x007fb2116b5178>):
app/controllers/roles_controller.rb:75:in `block in update'
app/controllers/roles_controller.rb:74:in `update'
To aid in my debugging I have logged the following:
@role: id[55], #<Role:0x007fb2137e3200>, #<Role id: 55, description: "T1", created_at: "2012-10-01 21:11:43", updated_at: "2012-10-01 21:11:43">, #<Role:0x007fb2137e3200>
@permission: [#<Permission id: 32, role_id: 55, subject_class: "Diagnosis", action: nil, subject_id: nil, created_at: "2012-10-01 21:11:43", updated_at: "2012-10-01 21:11:43">]
Is it because one is 'wrapped' as an array? [output of the @permission logger.debug call]. Is this the indicator that I should be using the update_all?
回答1:
Yes, #where()
returns an ActiveRecord::Relation, which can represent multiple records. You may just want to use @role.permissions(params[:permission]).first.update(...)
instead of update_all, since you only intend to update a single row.
Also, I noticed that you're implementing a permissions/role model. May I suggest a gem for this purpose since this has been reimplemented many times? Something like cancan/cantango, perhaps?
来源:https://stackoverflow.com/questions/12681371/update-fail-undefined-method-update-attributes-for-activerecordrelation