多个belongs_to关系到三个模型

发布于 2024-11-01 04:39:55 字数 767 浏览 1 评论 0原文

情况是这样的..

class Organization < ActiveRecord::Base

  has_many :role_memberships
  has_many :roles
  has_many :users, :through => :role_memberships, :uniq => true

end

class User

 has_many :role_memberships
  has_many :organizations, :through => :role_memberships, :uniq => true
  has_many :roles, :through => :role_memberships, :uniq => true 

end

class RoleMembership < ActiveRecord::Base

  belongs_to :organization
  belongs_to :role
  belongs_to :user

end

class Role < ActiveRecord::Base
  belongs_to :organization
  has_many :role_memberships
  has_many :users, :through => :role_memberships, :uniq => true
end

问题是如何填充角色成员表中的所有三个外键..当我执行 org.users.push(u) 时,这会创建一条记录,但 role_id 被遗漏了...

The situation is this way..

class Organization < ActiveRecord::Base

  has_many :role_memberships
  has_many :roles
  has_many :users, :through => :role_memberships, :uniq => true

end

class User

 has_many :role_memberships
  has_many :organizations, :through => :role_memberships, :uniq => true
  has_many :roles, :through => :role_memberships, :uniq => true 

end

class RoleMembership < ActiveRecord::Base

  belongs_to :organization
  belongs_to :role
  belongs_to :user

end

class Role < ActiveRecord::Base
  belongs_to :organization
  has_many :role_memberships
  has_many :users, :through => :role_memberships, :uniq => true
end

The QUESTION is how do I populate all the three foreign-keys in rolemembership table..when I do org.users.push(u) this create a record but role_id is left out...

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

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

发布评论

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

评论(1

请恋爱 2024-11-08 04:39:55

在这种情况下,我可能会创建 RoleMembership 对象本身,如下所示:

RoleMembership.create(:organization_id => org.id, :role_id => role.id, :user_id => user.id)

In this case I will probably create RoleMembership object itself, like this:

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