Ruby-on-rails Rails has_one : 通过关联
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2116017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails has_one :through association
提问by Anurag
Rails has a has_one :throughassociation that helps set up a one-to-one association with a third model by going through a second model. What is the real use of that besides making a shortcut association, that would otherwise be an extra step away.
Rails 有一个has_one :through关联,可以通过第二个模型帮助建立与第三个模型的一对一关联。除了建立快捷方式关联之外,它的真正用途是什么,否则会多走一步。
Taking this example from the Rails guide:
从 Rails指南中获取此示例:
class Supplier < ActiveRecord::Base
has_one :account
has_one :account_history, :through => :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
has_one :account_history
end
class AccountHistory < ActiveRecord::Base
belongs_to :account
end
might allow us to do something like:
可能允许我们执行以下操作:
supplier.account_history
which would otherwise be reached as:
否则将达到:
supplier.account.history
If it's only for simpler access then technically there could be a one-to-one association that connects a model with some nth model going through n-1 models for easier access. Is there anything else to it that I am missing besides the shortcut?
如果只是为了更简单的访问,那么从技术上讲,可能存在一对一关联,将模型与通过 n-1 个模型的某个第 n 个模型连接起来,以便于访问。除了快捷方式之外,我还缺少其他什么吗?
采纳答案by Staelen
Logic, OK it might sound a bit weak for this but it would be logical to say that "I have a supplier who has an account with me, I want to see the entire account history of this supplier", so it makes sense for me to be able to access account history from supplier directly.
Efficiency, this for me is the main reason I would use
:through, simply because this issues a join statement rather than calling supplier, and then account, and then account_history. noticed the number of database calls?using
:through, 1 call to get the supplier, 1 call to get account_history (rails automatically uses:jointo retrieve through account)using normal association, 1 call to get supplier, 1 call to get account, and 1 call to get account_history
逻辑,好吧,这听起来可能有点弱,但是说“我有一个供应商在我这里有一个帐户,我想查看该供应商的整个帐户历史记录”是合乎逻辑的,所以这对我来说很有意义能够直接从供应商访问帐户历史记录。
效率,这对我来说是我会使用的主要原因
:through,仅仅是因为这会发出一个 join 语句,而不是调用供应商,然后是帐户,然后是 account_history。注意到数据库调用的次数了吗?using
:through,1 次调用获取供应商,1 次调用获取 account_history(rails 自动用于:join通过帐户检索)使用正常关联,1 次调用获取供应商,1 次调用获取帐户,1 次调用获取 account_history
That's what I think =) hope it helps!
这就是我的想法=)希望它有所帮助!
回答by ryan0
I'm surprised no one has touched on Association Objects.
我很惊讶没有人接触过Association Objects。
A has_many(or has_one) :throughrelationship facilitates the use of the association object patternwhich is when you have two things related to each other, and that relation itself has attributes (ie a date when the association was made or when it expires).
A has_many(or has_one):through关系促进了关联对象模式的使用,即当您有两个相互关联的事物时,该关系本身具有属性(即建立关联的日期或到期的日期)。
This is considered by someto be a good alternative to the has_and_belongs_to_manyActiveRecord helper. The reasoning behind this is that it is very likely that you will need to change the nature of the association or add to it, and when you are a couple months into a project, this can be very painful if the relationship were initially set up as a has_and_belongs_to_many(the second link goes into some detail). If it is set up initially using a has_many :throughrelationship, then a couple months into the project it's easy to rename the join model or add attributes to it, making it easier for devs to respond to changing requirements. Plan for change.
有些人认为这是has_and_belongs_to_manyActiveRecord 助手的一个很好的替代品。这背后的原因是您很可能需要更改关联的性质或添加关联,并且当您进入一个项目几个月时,如果最初将关系设置为,这可能会非常痛苦a has_and_belongs_to_many(第二个链接详细介绍)。如果最初使用has_many :through关系进行设置,那么在项目进行几个月后,很容易重命名连接模型或为其添加属性,从而使开发人员更容易响应不断变化的需求。计划改变。
回答by 0x4a6f4672
Inverse association: consider the classic situation user-membership-group. If a user can be a member in many groups, then a group has many members or users, and a user has many groups. But if the user can only be a member in one group, the group still has many members:
class User has_one :group, :through => :membershipbutclass Group has_many :members, :through => memberships. The intermediate modelmembershipis useful to keep track of the inverse relationship.Expandability: a
has_one :throughrelationship can easy be expanded and extended to ahas_many :throughrelationship
反向关联:考虑用户-会员-组的经典情况。如果一个用户可以是多个组的成员,那么一个组就有多个成员或用户,一个用户也有多个组。但是如果用户只能是一个组的成员,则该组仍然有很多成员:
class User has_one :group, :through => :membership但是class Group has_many :members, :through => memberships. 中间模型membership对于跟踪逆关系很有用。可扩展性:一个
has_one :through关系可以很容易地扩展和扩展为一个has_many :through关系

