class Membership < ActiveRecord::Base
belongs_to :role
belongs_to :user
end
class User < ActiveRecord::Base
has_many :roles, :through => :memberships
end
class Role < ActiveRecord::Base
has_many :users, :through => :memberships
end
和我的观点
<% for role in Role.find(:all) %>
<div>
<%=check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %>
<%=role.name%>
</div>
<% end %>
我的视图上有下一个错误 - 无法找到关联:模型用户中的成员资格 我无法理解为什么会发生这种情况..
答案 0 :(得分:15)
您需要明确说明has_many :memberships
,如下所示:
class User < ActiveRecord::Base
has_many :memberships
has_many :roles, :through => :memberships
end
class Role < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end
添加它,你应该启动并运行。
答案 1 :(得分:0)
我找到了原因,
我需要添加
has_many :memberships
到我的用户和角色模型。
非常感谢! :)