This is mainly a theoritical question, but just wanted to make sure that i'm doing it correctly. Consider a Ruby Rails project where there are two models, one being User and the other being Alliance.
这主要是一个理论问题,但只是想确保我正确地做到了。考虑一个Ruby Rails项目,其中有两个模型,一个是User,另一个是Alliance。
A user can only have one alliance. An alliance can have many users.
用户只能拥有一个联盟。联盟可以拥有许多用户。
That is very much simply a :has_many, :belongs_to relationship.
这非常简单:a:has_many,:belongs_to relationship。
However, i feel that using an intermediate model is a better way to do it. That would be a :has_many :through :users_alliances, where users_alliances would be an intermediate model.
但是,我觉得使用中间模型是一种更好的方法。那将是:has_many:through:users_alliances,其中users_alliances将是一个中间模型。
What do you think is the best way to do that ?
您认为最好的方法是什么?
2 个解决方案
#1
2
Don't use habtm, it doesn't make the join into a fully fledged model, which is what you need. Use has_many :through on the alliance side and has_one :through on the user side.
不要使用habtm,它不会使连接成为一个完全成熟的模型,这是你需要的。在联盟方使用has_many:through,在用户端使用has_one:through。
User
has_one :alliance_membership
has_one :alliance, :through => :alliance_membership
AllianceMembership
belongs_to :user
belongs_to :alliance
Alliance
has_many :alliance_memberships
has_many :users, :through => :alliance_memberships
Personally i prefer the class name AllianceMembership to UserAlliance but you could use either. UsersAlliances is ugly though imo. (another advantage of has_many/one :through is that you can call the join table/class whatever you want).
我个人更喜欢班级名称AllianceMembership到UserAlliance,但你可以使用其中任何一个。用户联盟虽然是imo,但是很难看。 (has_many / one的另一个优点:通过你可以随意调用连接表/类)。
This gives you the option to easily change to users having many alliances later on if you want.
这使您可以选择在以后轻松更改为具有多个联盟的用户。
#2
0
Why do you think that? What do you think you'll gain from the join model?
为什么你这么想?您认为从连接模型中获得什么?
Personally I doubt the extra table/model would be helpful, unless you think that one-to-many will become many-to-many in the future.
我个人怀疑额外的表/模型会有所帮助,除非你认为一对多将在未来成为多对多。
#1
2
Don't use habtm, it doesn't make the join into a fully fledged model, which is what you need. Use has_many :through on the alliance side and has_one :through on the user side.
不要使用habtm,它不会使连接成为一个完全成熟的模型,这是你需要的。在联盟方使用has_many:through,在用户端使用has_one:through。
User
has_one :alliance_membership
has_one :alliance, :through => :alliance_membership
AllianceMembership
belongs_to :user
belongs_to :alliance
Alliance
has_many :alliance_memberships
has_many :users, :through => :alliance_memberships
Personally i prefer the class name AllianceMembership to UserAlliance but you could use either. UsersAlliances is ugly though imo. (another advantage of has_many/one :through is that you can call the join table/class whatever you want).
我个人更喜欢班级名称AllianceMembership到UserAlliance,但你可以使用其中任何一个。用户联盟虽然是imo,但是很难看。 (has_many / one的另一个优点:通过你可以随意调用连接表/类)。
This gives you the option to easily change to users having many alliances later on if you want.
这使您可以选择在以后轻松更改为具有多个联盟的用户。
#2
0
Why do you think that? What do you think you'll gain from the join model?
为什么你这么想?您认为从连接模型中获得什么?
Personally I doubt the extra table/model would be helpful, unless you think that one-to-many will become many-to-many in the future.
我个人怀疑额外的表/模型会有所帮助,除非你认为一对多将在未来成为多对多。