[英]Object with more than one Model (Rails)
BACKGROUND: I'm thinking conceptually about how to reference a single object that can belong to more than one model. 背景:我在概念上思考如何引用可以属于多个模型的单个对象。 For instance, if I have a School and Employer model - a User could theoretically have attended and be employed at the same School.
例如,如果我有一个School and Employer模型-一个用户理论上可以参加并在同一所学校工作。 Ideally both relationships to point to the same object.
理想情况下,两个关系都指向同一对象。
QUESTION: How would I model that in a postgresql-based Rails app? 问题:如何在基于PostgreSQL的Rails应用程序中对其建模?
UPDATE In this case, I'd need a user to be able to have both one or more Schools and one or more Employers. 更新在这种情况下,我需要一个用户同时拥有一所或多所学校和一个或多个雇主。
EDIT: To reflect that the question was about HABTM 编辑:以反映该问题是关于HABTM
You'd use something like this if you want to be able to use the same exact record for both Employer and School in User.employer
. 如果您希望能够对
User.employer
Employer和School使用相同的准确记录,则可以使用User.employer
。 I'm assuming a User can only have one School and one Employer, no more. 我假设一个用户只能拥有一所学校和一个雇主,不能再拥有更多。
class School < ActiveRecord::Base
has_many :employees, as: :employable
has_and_belongs_to_many :students, class_name: "User"
end
class Business < ActiveRecord::Base
has_many :employees, as: :employable
end
class Contract < ActiveRecord::Base #This object models the relationship between User and Employer
belongs_to :employer, as: :employable, polymorphic: true
belongs_to :user
class User < ActiveRecord::Base
has_and_belongs_to_many :schools
has_many :contracts
has_many :employers, through: :contracts
end
this would set it up so that if you call User.school
it would return a School object, but if you call User.employer
it could go to either School or Employer depending on what User points to. 它将进行设置,以便如果您调用
User.school
它将返回一个School对象,但是如果您调用User.employer
则可以根据用户指向的位置转到School或Employer。
This makes use of Polymorphic Associations . 这利用了多态关联 。 Please read about them as the Migrations are different for this kind of association.
请阅读有关它们的信息,因为这种关联的迁移方式有所不同。
The reason we're using a Contract
object is that a has_and_belongs_to_many
association can't be polymorphic, so we to roll that out manually. 我们使用
Contract
对象的原因是has_and_belongs_to_many
关联不能是多态的,因此我们手动将其推出。 If you don't like has_and_belongs_to_many
or need more flexibility for the School
relationship, you could use an intermediary object like for employer, maybe called Tuition
. 如果您不喜欢
has_and_belongs_to_many
或需要更多的灵活性来处理School
关系,则可以使用像雇主这样的中介对象,也许称为Tuition
。
To do this you would have three models, User, School and Employer. 为此,您将拥有三个模型:用户,学校和雇主。
The User belongs_to
one School and belongs_to
one Employer. 用户
belongs_to
一个学校, belongs_to
一个雇主。 The School has_many
Users. 学校
has_many
用户。 The Employer has_many
Users. 雇主
has_many
用户。
Knowing these relations, which are used directly in your models, you now know that the User will have a school_id
and an employer_id
attribute. 知道了这些关系,这是直接用于你的模型,你现在知道了用户将有一个
school_id
和employer_id
属性。 Neither the School nor the Employer will have a user_id
. 学校和雇主都不会有
user_id
。
If you decide to go down the route of users having and belonging to many Schools or Employers ( has_and_belongs_to_many
) then you will want to look at the ActiveRecord documentation on join tables. 如果您决定
has_and_belongs_to_many
拥有并属于许多学校或雇主( has_and_belongs_to_many
)的用户的路线,那么您将需要查看has_and_belongs_to_many
表上的ActiveRecord文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.