如何访问 has_many :through 关系上的连接模型

发布于 2024-08-22 02:57:54 字数 1209 浏览 10 评论 0原文

我有一个典型的多对多关系,使用 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 技术交流群。

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

发布评论

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

评论(2

世界如花海般美丽 2024-08-29 02:57:54

您应该使用 member_roles 关联而不是 roles 关联。

  def update
    # The role hash should contain :role_id, :scope and if set, :sport_id
    roles = ((params[:member] ||= {}).delete "roles") || []
    MemberRole.transaction do 

      # Next line will associate the :sport_id and :scope to member_roles
      # EDIT: Changed the code to flush old roles.
      @member.member_roles = roles.collect{|r| MemberRole.new(r)}

      # Next line will save the member attributes and member_roles in one TX
      if @member.update_attributes params[:member]
        # some code
      else
        # some code
      end
    end
  end

确保将所有内容都包含在一笔交易中。

另一种可能性是在 member_roles 关联上使用 accepts_nested_attributes_for

You should use the member_roles association instead of roles association.

  def update
    # The role hash should contain :role_id, :scope and if set, :sport_id
    roles = ((params[:member] ||= {}).delete "roles") || []
    MemberRole.transaction do 

      # Next line will associate the :sport_id and :scope to member_roles
      # EDIT: Changed the code to flush old roles.
      @member.member_roles = roles.collect{|r| MemberRole.new(r)}

      # Next line will save the member attributes and member_roles in one TX
      if @member.update_attributes params[:member]
        # some code
      else
        # some code
      end
    end
  end

Make sure you enclose everything in one transaction.

Other possibility is to use accepts_nested_attributes_for on member_roles association.

痕至 2024-08-29 02:57:54

听起来你应该考虑使用嵌套属性:

Sounds like you should look into using nested attributes:

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