保存 has_and_belongs_to_many 孩子

发布于 2024-07-16 04:07:45 字数 1645 浏览 6 评论 0原文

我有一个用户模型和一个角色模型。 它们通过 has_and_belongs_to_many 关系连接起来。 当管理员创建用户时,我希望他们能够为用户分配角色,并在我调用 @user.save 时保存该角色,

但问题是我收到一条警告,指出我无法批量分配角色关系。

关于如何解决这个问题的任何建议,我正在使用 Rails 2.3.2

谢谢。

编辑:按要求编码。

user.rb

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles,
                          :join_table => "users_roles",
                          :foreign_key => "role_id",
                          :associated_foreign_key => "user_id"
end

role.rb

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users,
                          :join_table => "users_roles", 
                          :foreign_key => "user_id", 
                          :association_foreign_key => "role_id"
end

视图:new.html.haml

- form_for(@user, users_path(:subdomain => current_account.subdomain)) do |f|
  .input_set
    = f.label(:login, "Username")
    = f.text_field(:login)
  .input_set
    = f.label(:name)
    = f.text_field(:name)
  .input_set
    = f.label(:email)
    = f.text_field(:email)
    - fields_for("user[roles][]", Role)do |user_role|
      .input_set
        %label Role
        = user_role.select(:name, Role.all.map{|r| [r.name, r.id] })
  .input_set
    = f.label(:password)
    = f.password_field(:password)
  .input_set
    = f.label(:password_confirmation, "Password Again")
    = f.password_field(:password_confirmation)
  .input_set
    %label
    = f.submit "Add User"

我希望通过在创建选项中调用 @user.save 将角色保存给用户。 那可能吗? 或者这是我不能以这种方式使用的关系,是否需要 has_many 关系才能做到这一点。

I have a User model and a Role model. They are joined by a has_and_belongs_to_many relationship. When an admin creates a user I want them to be able to assign a role to the user and have it saved when I call @user.save

The thing is though is that I get a warning that I can't mass-assign the roles relationship.

Any suggestions on how to go about this, I am on Rails 2.3.2

Thanks.

Edit: Code as requested.

user.rb

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles,
                          :join_table => "users_roles",
                          :foreign_key => "role_id",
                          :associated_foreign_key => "user_id"
end

role.rb

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users,
                          :join_table => "users_roles", 
                          :foreign_key => "user_id", 
                          :association_foreign_key => "role_id"
end

View: new.html.haml

- form_for(@user, users_path(:subdomain => current_account.subdomain)) do |f|
  .input_set
    = f.label(:login, "Username")
    = f.text_field(:login)
  .input_set
    = f.label(:name)
    = f.text_field(:name)
  .input_set
    = f.label(:email)
    = f.text_field(:email)
    - fields_for("user[roles][]", Role)do |user_role|
      .input_set
        %label Role
        = user_role.select(:name, Role.all.map{|r| [r.name, r.id] })
  .input_set
    = f.label(:password)
    = f.password_field(:password)
  .input_set
    = f.label(:password_confirmation, "Password Again")
    = f.password_field(:password_confirmation)
  .input_set
    %label
    = f.submit "Add User"

And I want the Role to be saved to the user by calling @user.save in my create option. Is that possible? Or is this a relationship I can't use that way, would it need to a has_many relationship for me to be able to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

第七度阳光i 2024-07-23 04:07:45

我相信您需要对要保存的模型中的属性调用 attr_accessible 以避免批量分配错误。

I believe you need to call attr_accessible on the attributes in the model that you want to save in order to avoid the mass-assign error.

池予 2024-07-23 04:07:45

您不能将accepts_nested_attributes_for 用于habtm 关系。

但是,您可以设置 role_ids,有关详细信息,请参阅 Railscast Episode 17

在您的情况下,问题是您只设置了一个角色,但具有 habtm 关系,为什么不使用 own_to 呢?

You cannot use accepts_nested_attributes_for for a habtm relationship.

You can however set the role_ids, see Railscast Episode 17 for details

In your case the problem is that you set only a single role but have a habtm relationship, why not a belongs_to?

情绪 2024-07-23 04:07:45

您是否使用新的 accepts_nested_attributes_for 方法?

它可能看起来像这样:

class User < ActiveRecord::Base 
  accepts_nested_attributes_for :roles, :allow_destroy => true
end

查看此示例应用程序以了解更多详细信息。< /a>

Are you using the new accepts_nested_attributes_for method?

It will probably look something like this:

class User < ActiveRecord::Base 
  accepts_nested_attributes_for :roles, :allow_destroy => true
end

Check out this sample app for more detail.

云之铃。 2024-07-23 04:07:45

考虑到问题提出以来的时间,您可能已经自己解决了这个问题......

问题是我得到了
警告我无法批量分配
角色关系。

这是由用户模型中的两件事之一引起的。

  1. 您调用了 attr_accessible,并且提供的符号列表不包括 :roles
  2. 您调用了 attr_protected,并且符号列表包括 :roles

将 :roles 添加到 attr_accessible 调用中,或者将其从 attr_protected 调用中删除。

Given the time since the question was asked, you've probably worked this out on your own...

The thing is though is that I get a
warning that I can't mass-assign the
roles relationship.

This is caused by one of two things in your User model.

  1. You called attr_accessible and the list of symbols provide does not include :roles
  2. You called attr_protected and the list of symbols includes :roles

Either add :roles to your attr_accessible call or remove it from the attr_protected call.

穿透光 2024-07-23 04:07:45

如果我可以编辑/添加答案,我会的。 我有类似@EmFi 提到的要求。 我设置了 attr_accessible,并且必须将等效项添加

:role_ids

到用户模型的 attr_accessible 中。 注意复数形式。 以下选项不起作用

  • :role
  • :roles
  • :role_id

只是为了清楚我收到的错误消息:

WARNING: Can't mass-assign these protected attributes: role_ids

该警告对我来说没有多大意义,因为我正在使用习惯关系。 尽管如此,这是正确的。

If I could edit/add to an answer I would. I had something similar required to what @EmFi mentioned. I had attr_accessible set, and had to add the equivalent of

:role_ids

to the attr_accessible of the user model. Note the pluralization. The following options did not work:

  • :role
  • :roles
  • :role_id

Just to be clear about the error message that I got:

WARNING: Can't mass-assign these protected attributes: role_ids

The warning didn't make a lot of sense to me since I'm using a habtm relationship. Nevertheless, it was correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文