如何访问 has_many :through 关系上的连接模型
我有一个典型的多对多关系,使用 has_many =>; :through
,详情如下。
class member
has_many member_roles
has_many roles, :through => :member_roles
end
class role
has_many member_roles
has_man members, :through => :member_roles
end
class member_role
belongs_to :member
belongs_to :role
# has following fields: member_id, role_id, scope, sport_id
end
我在这里想做的是允许为成员分配角色。每个成员角色都有一个范围,默认情况下设置为“全部”,但如果需要,可以设置为“运动”。如果范围设置为 sport,那么我们还会捕获 sport_id,这使我们能够将该角色的评估限制为特定运动(即只能管理该运动的团队,而不是每个运动的团队)。听起来很简单。
我已经设置了我的 update_member_roles
操作,如下所示:
def update
# Assume we passing a param like: params[:member][:roles]
# as an array of hashes consisting of :role_id and if set, :sport_id
roles = (params[:member] ||= {}).delete "roles"
@member.roles = Role.find_all_by_id(roles.map{|r| r["role_id"]})
if @member.update_attributes params[:member]
flash[:notice] = "Roles successfully updated."
redirect_to member_path(@member)
else
render :action => "edit"
end
end
上面的内容足够好,它很好地设置了适当的member_roles...但是当我正在研究角色模型而不是MemberRole模型时,我'我对如何访问连接模型来设置 :scope 和 :sport_id 有点困惑。
任何指点这里将不胜感激。
I have a typical many-to-many relationship using has_many => :through
, as detailed below.
class member
has_many member_roles
has_many roles, :through => :member_roles
end
class role
has_many member_roles
has_man members, :through => :member_roles
end
class member_role
belongs_to :member
belongs_to :role
# has following fields: member_id, role_id, scope, sport_id
end
What I'm trying to do here is allow members to be assigned roles. Each member role has a scope, which by default is set to "All" but if desired can be set to "Sport". If the scope is set to sport then we also capture the sport_id, which allows us to restrict assess on that role to a particular sport (ie, can only manage the teams of that sport, rather than the teams of every sport). Sounds simple enough.
I have setup my update_member_roles
action something like this:
def update
# Assume we passing a param like: params[:member][:roles]
# as an array of hashes consisting of :role_id and if set, :sport_id
roles = (params[:member] ||= {}).delete "roles"
@member.roles = Role.find_all_by_id(roles.map{|r| r["role_id"]})
if @member.update_attributes params[:member]
flash[:notice] = "Roles successfully updated."
redirect_to member_path(@member)
else
render :action => "edit"
end
end
The above works nice enough, it sets the appropriate member_roles very nicely... but as I'm working on the Role model and not the MemberRole model I'm kind of stuck as to how I can access the joining model to set the :scope and :sport_id.
Any pointers here would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
member_roles
关联而不是roles
关联。确保将所有内容都包含在一笔交易中。
另一种可能性是在
member_roles
关联上使用accepts_nested_attributes_for
。You should use the
member_roles
association instead ofroles
association.Make sure you enclose everything in one transaction.
Other possibility is to use
accepts_nested_attributes_for
onmember_roles
association.听起来你应该考虑使用嵌套属性:
Sounds like you should look into using nested attributes: