2012-10-01 52 views
1

這從我以前的要求,我回答了。但我不明白的下一個問題是爲什麼當我的參數報告一條記錄時,我會收到一條消息,指出Google搜索暗示我需要使用update_all。更新失敗 - 未定義的方法`update_attributes'的#<ActiveRecord ::關係:

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

這個問題有嚴重的問題。您應該將標題編輯爲可以理解的內容,並嘗試在您的問題中提出具體問題。 –

+0

希望有一個更好的標題。如果不是,你可以請建議 – user1149642

回答

1

是的,#where()返回一個ActiveRecord :: Relation,它可以表示多個記錄。您可能只想使用@role.permissions(params[:permission]).first.update(...)而不是update_all,因爲您只打算更新單個行。

另外,我注意到你正在實現一個權限/角色模型。我可以爲此提出一個寶石,因爲這已經多次被重新實現了嗎?也許像cancan/cantango一樣?

相關問題