Rails has_many through 与独立的直通表

发布于 2024-09-24 17:03:50 字数 636 浏览 6 评论 0原文

我有一个用户模型、个人模型和公司模型。

一个用户通过个人拥有许多公司,反之亦然。

但我希望能够填充与稍后可以绑定的用户无关的人员和公司。

class User < ActiveRecord::Base
  attr_accessible :name
  has_many :people
  has_many :companies, :through => :people
end

class Person < ActiveRecord::Base
  attr_accessible :user_id, :company_id
  belongs_to :users
  belongs_to :companies
end

class Company < ActiveRecord::Base
  attr_accessible :name
  has_many :people
  has_many :users, :through => :person
end

现在在控制台中我想要执行以下操作

User.find(1).companies

,然后它应该找到我对 user(1) 感兴趣的公司。

我是否做错了,我应该做一些小的改变吗?

I have a User model, Person model and Company model.

a User has many companies through Person and vice versa.

But i would like to be able to populate People and Companies that are not tied to Users that can be tied later.

class User < ActiveRecord::Base
  attr_accessible :name
  has_many :people
  has_many :companies, :through => :people
end

class Person < ActiveRecord::Base
  attr_accessible :user_id, :company_id
  belongs_to :users
  belongs_to :companies
end

class Company < ActiveRecord::Base
  attr_accessible :name
  has_many :people
  has_many :users, :through => :person
end

now in the console i want to be doing the following

User.find(1).companies

then it should find me the companies in which user(1) is a person of interest.

Have I got this wrong, is there a small change that I should be making.

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

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

发布评论

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

评论(2

渡你暖光 2024-10-01 17:03:50

您的 Person 模型不能直接“belong_to”多个,您的 belongs_to :usersbelongs_to :companies 关联将无法以这种方式工作。公司与人员之间需要通过另一个描述它们之间关系的连接表进行连接,例如 Employment 指向每个模型的一个实例:

class Person < ActiveRecord::Base
  has_many :employments
  has_many :companies, :through => :employments
end

class Employment < ActiveRecord::Base
  belongs_to :person
  belongs_to :company
end

class Company < ActiveRecord::Base
  has_many :employments
  has_many :people, :through => :employments
end

然后您可以使用 :through< /code> 选项将雇佣关系另一方的许多公司/人员关联到中间。

同样,如果一个人可以由多个用户拥有,那么您也需要这两个实体之间的连接模型。

Your Person model can't directly "belong_to" more than one, your belongs_to :users and belongs_to :companies associations won't work that way. Companies-to-people need to be connected through another join table that describes the relationship between them, for example Employment which points to one instance of each model:

class Person < ActiveRecord::Base
  has_many :employments
  has_many :companies, :through => :employments
end

class Employment < ActiveRecord::Base
  belongs_to :person
  belongs_to :company
end

class Company < ActiveRecord::Base
  has_many :employments
  has_many :people, :through => :employments
end

You can then use the :through option to associate the many companies/people on the other side of that employment relationship in the middle.

Similarly, if a Person can be owned by more than one User then you will need a join model between those two entities as well.

尘世孤行 2024-10-01 17:03:50

作为后续,在 has_many :through 关系中,没有任何内容表明您不能独立使用连接表(在您的情况下,Person)。根据关系的性质,您将通过完全独立的 ActiveRecord 模型加入,这是它与 has_and_belongs_to_many 关系最显着的区别。

正如布拉德在他的评论中指出的那样,在你们的关系中,你需要将“人”多元化为“人”。除此之外,看起来您的设置是正确的。使用 attr_accessible 公开 :user_id:company_id 将使您能够稍后从回发中批量分配这些值,但很多时候您想要害羞不要使用基于角色的关联来这样做,因为您可能不希望让它们暴露于潜在的 HTTP Post 攻击。

请记住,在您的控制器中,您始终可以使用或不使用 attr_accessible 执行类似的操作:

@person = Person.new
@person.user = User.find(...)
@person.company = Company.find(...)
@person.save

希望有所帮助。

Just as a followup, in a has_many :through relationship, there is nothing that says you cannot use your join table (in your case, Person) independently. By nature of the relationship, you are joining through a completely separate ActiveRecord model, which is what most notably distinguishes it from the has_and_belongs_to_many relationship.

As Brad pointed out in his comment, you need to pluralize 'person' to 'people' in your relationship. Other than that, it looks like you set it up correctly. Exposing :user_id and :company_id with attr_accessible will enable you to mass-assign these values later from a postback, but often times you want to shy away from doing so with role-based associations, as you may not want to leave them exposed to potential HTTP Post attacks.

Remember, in your controller you can always do something like this with or without attr_accessible:

@person = Person.new
@person.user = User.find(...)
@person.company = Company.find(...)
@person.save

Hope that helps.

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