1

这是继我之前回答的请求之后的结果。但我无法理解的下一个问题是,为什么当我的参数报告一条记录时,我会收到一条消息,即谷歌搜索/SO 搜索建议我需要使用 update_all。

has_many/belongs_to 建立关联 - 为什么参数的插入语句为空白?

我的更新控制器方法如下:

  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 的指标吗?


浮动可以比这更高 - 例如尝试:

    float x = 1000000000000000000000000000000000000.0f; 
    System.out.println(x);
    System.out.println(x*x);

输出:

1.0E36
Infinity

所以还有其他问题。请注意,使用非常大的数字会导致Infinitynot NaN

您的代码是否无意中将零除以零或类似的东西?您正在执行的实际计算是什么?

另请参阅此问题以获取有关 NaN 含义的一些信息。

4

1 回答 1

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?

于 2012-10-10T20:53:07.550 回答