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. 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.
更新在这种情况下,我需要一个用户能够拥有一个或多个学校和一个或多个雇主。
2 个解决方案
#1
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
. I'm assuming a User can only have one School and one Employer, no more.
如果您希望能够在User.employer中为雇主和学校使用相同的确切记录,则可以使用此类内容。我假设一个用户只能有一个学校和一个雇主,不再有。
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它可以转到学校或雇主,具体取决于用户指向的内容。
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. 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
.
我们使用Contract对象的原因是has_and_belongs_to_many关联不能是多态的,所以我们手动推出它。如果您不喜欢has_and_belongs_to_many或需要更多的学校关系灵活性,您可以使用像雇主这样的中间对象,也许称为学费。
#2
To do this you would have three models, User, School and Employer.
要做到这一点,你将有三个模型,用户,学校和雇主。
The User belongs_to
one School and belongs_to
one Employer. The School has_many
Users. The Employer has_many
Users.
用户belongs_toone学校并且属于一个雇主。学校有很多用户。雇主有很多用户。
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. Neither the School nor the Employer will have a user_id
.
了解这些直接在模型中使用的关系,您现在知道用户将拥有school_id和employer_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),那么您将需要查看关于连接表的ActiveRecord文档。
#1
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
. I'm assuming a User can only have one School and one Employer, no more.
如果您希望能够在User.employer中为雇主和学校使用相同的确切记录,则可以使用此类内容。我假设一个用户只能有一个学校和一个雇主,不再有。
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它可以转到学校或雇主,具体取决于用户指向的内容。
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. 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
.
我们使用Contract对象的原因是has_and_belongs_to_many关联不能是多态的,所以我们手动推出它。如果您不喜欢has_and_belongs_to_many或需要更多的学校关系灵活性,您可以使用像雇主这样的中间对象,也许称为学费。
#2
To do this you would have three models, User, School and Employer.
要做到这一点,你将有三个模型,用户,学校和雇主。
The User belongs_to
one School and belongs_to
one Employer. The School has_many
Users. The Employer has_many
Users.
用户belongs_toone学校并且属于一个雇主。学校有很多用户。雇主有很多用户。
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. Neither the School nor the Employer will have a user_id
.
了解这些直接在模型中使用的关系,您现在知道用户将拥有school_id和employer_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),那么您将需要查看关于连接表的ActiveRecord文档。