has_many/belongs_to build association - why is insert statement for parameter blank?

左心房为你撑大大i 提交于 2019-12-12 03:09:12

问题


I have the following models

role.rb

class Role < ActiveRecord::Base
  attr_accessible :description, :user_id, :role_ids, :permission_ids, :permissions

  has_many :assignments
  has_many :users, :through => :assignments
  has_many :permissions

  validates :description, presence: true, 
                          uniqueness: true

end

permission.rb

class Permission < ActiveRecord::Base
  attr_accessible :action, :role_id, :subject_class, :subject_id, :role

  belongs_to :role, :polymorphic => true
end

My roles_controller.rb is as follows:

  def new
    prepare
    @role = Role.new
    # @permission = @role.permissions.build


    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @role }
    end
  end

  def create
    @role = Role.new(params[:role])
    @permissions = @role.permissions.build(params[:permissions])
    p params

    respond_to do |format|
      if @role.save
        format.html { redirect_to @role, notice: 'Role was successfully created.' }
        format.json { render json: @role, status: :created, location: @role }
      else
        format.html { render action: "new" }
        format.json { render json: @role.errors, status: :unprocessable_entity }
      end
    end
  end

When I submit this I get the following console output.

Started POST "/roles" for 127.0.0.1 at 2012-10-01 11:26:03 +0100
Processing by RolesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3uZ47itmgzUSs83Iq9+2saJn6Y9+Y9tlSDgaRskh9pw=", "role"=>{"description"=>"T1"}, "permission"=>{"subject_class"=>"CancanPermission"}, "commit"=>"Create Role"}
  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
   (0.0ms)  begin transaction
  Role Exists (0.1ms)  SELECT 1 FROM "roles" WHERE "roles"."description" = 'T1' LIMIT 1
  SQL (0.4ms)  INSERT INTO "roles" ("created_at", "description", "updated_at") VALUES (?, ?, ?)  [["created_at", Mon, 01 Oct 2012 10:26:03 UTC +00:00], ["description", "T1"], ["updated_at", Mon, 01 Oct 2012 10:26:03 UTC +00:00]]
  SQL (0.2ms)  INSERT INTO "permissions" ("action", "created_at", "role_id", "subject_class", "subject_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["action", nil], ["created_at", Mon, 01 Oct 2012 10:26:03 UTC +00:00], ["role_id", 47], ["subject_class", nil], ["subject_id", nil], ["updated_at", Mon, 01 Oct 2012 10:26:03 UTC +00:00]]
   (1.7ms)  commit transaction
Redirected to http://localhost:3005/roles/47
Completed 302 Found in 19ms (ActiveRecord: 3.0ms)


Started GET "/roles/47" for 127.0.0.1 at 2012-10-01 11:26:03 +0100
Processing by RolesController#show as HTML
  Parameters: {"id"=>"47"}
  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", "47"]]
  Rendered roles/show.html.erb within layouts/application (0.7ms)
  Rendered layouts/_header.html.erb (1.5ms)
  Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 9ms (Views: 6.2ms | ActiveRecord: 0.4ms)

My subject_class parameter in permission has a value [CancanPermission], but it doesn't get inserted in the permissions table. I only get the role_id from the build association.

Can somebody guide me as to what I'm doing wrong?


回答1:


Here is what I did following some more googling & SO searching.

I changed the following line in my create action in the roles_controller.rb as follows:

From:

@permissions = @role.permissions.build(params[:permissions])

To:

@permissions = @role.permissions.build(params[:permission])

Hey presto, it worked!

But using the same line in my edit I get new entries in the permissions table, but the updated version. So what is the syntax for edit/update?



来源:https://stackoverflow.com/questions/12671646/has-many-belongs-to-build-association-why-is-insert-statement-for-parameter-bl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!