这是我之前提出的请求之后的回答。但我的下一个问题是我无法理解的,为什么当我的参数报告一条记录时,我会收到一条谷歌搜索消息/搜索提示我需要使用update_all。
has_many/belongs_to build association - why is insert statement for parameter blank?
我的更新控制器方法如下:
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
我收到以下错误消息:
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"}
谷歌搜索和SO搜索显示我可能需要使用[update_all
]。但是为什么当我只有一个记录时就是这种情况呢?
这是我的控制台输出。
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'
为了帮助我进行调试,我记录了以下内容:
@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">]
是因为一个人被'包裹'成阵列吗? [@permission logger.debug调用的输出]。这是我应该使用update_all吗?
的指标答案 0 :(得分:1)
是的,#where()
返回一个ActiveRecord :: Relation,它可以代表多条记录。您可能只想使用@role.permissions(params[:permission]).first.update(...)
而不是update_all,因为您只打算更新单行。
此外,我注意到您正在实施权限/角色模型。我可以为此目的建议一个宝石,因为这已经多次重新实现了吗?或许像cancan / cantango这样的东西?