保存 has_and_belongs_to_many 孩子
我有一个用户模型和一个角色模型。 它们通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信您需要对要保存的模型中的属性调用 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.
您不能将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?
您是否使用新的 accepts_nested_attributes_for 方法?
它可能看起来像这样:
查看此示例应用程序以了解更多详细信息。< /a>
Are you using the new accepts_nested_attributes_for method?
It will probably look something like this:
Check out this sample app for more detail.
考虑到问题提出以来的时间,您可能已经自己解决了这个问题......
这是由用户模型中的两件事之一引起的。
将 :roles 添加到 attr_accessible 调用中,或者将其从 attr_protected 调用中删除。
Given the time since the question was asked, you've probably worked this out on your own...
This is caused by one of two things in your User model.
Either add :roles to your attr_accessible call or remove it from the attr_protected call.
如果我可以编辑/添加答案,我会的。 我有类似@EmFi 提到的要求。 我设置了 attr_accessible,并且必须将等效项添加
到用户模型的 attr_accessible 中。 注意复数形式。 以下选项不起作用:
只是为了清楚我收到的错误消息:
该警告对我来说没有多大意义,因为我正在使用习惯关系。 尽管如此,这是正确的。
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
to the attr_accessible of the user model. Note the pluralization. The following options did not work:
Just to be clear about the error message that I got:
The warning didn't make a lot of sense to me since I'm using a habtm relationship. Nevertheless, it was correct.