Rails has_many:通过 has_many:通过
我想知道我可以在 Rails 中使用关联到什么程度。考虑以下因素:
class User < ActiveRecord::Base
has_one :provider
has_many :businesses, :through => :provider
end
class Provider < ActiveRecord::Base
has_many :businesses
has_many :bids, :through => :businesses
belongs_to :user
end
class Business < ActiveRecord::Base
has_many :bids
belongs_to :provider
end
class Bid < ActiveRecord::Base
belongs_to :business
end
我可以设置这些漂亮的快捷方式,例如 User.businesses
和 Provider.bids
,但是执行 User.bids< 之类的操作怎么样? /代码>?可以这么说吗?
I'm wondering to what extent I can use associations in Rails. Take into consideration the following:
class User < ActiveRecord::Base
has_one :provider
has_many :businesses, :through => :provider
end
class Provider < ActiveRecord::Base
has_many :businesses
has_many :bids, :through => :businesses
belongs_to :user
end
class Business < ActiveRecord::Base
has_many :bids
belongs_to :provider
end
class Bid < ActiveRecord::Base
belongs_to :business
end
I am able to set up these nifty shortcuts like User.businesses
and Provider.bids
but what about doing something like User.bids
? Is it possible to associate an association, so to speak?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是完全可能的,但需要一些额外的工作。以下模型定义与 nested_has_many 插件 结合使用,您只需使用 < code>@user.bids
然而,从出价中获取用户需要更多的工作。
This is entirely possible, but needs a little extra work. The following model definitions used in conjunction with the nested_has_many plugin you can fetch all bids belonging to a user with just
@user.bids
However getting a user from a bid will take a more work.
如果您只想获取记录,为什么不使用
#delegate
?它工作得很好,至少在你描述的场景中是这样。
尽管在我不那么谦虚的观点中,你应该只链接这些方法,因为它更简单,并且除非你像 tadman 所说的那样使用一些疯狂的自定义 SQL,否则你将不再实现性能提升。
If you just want to fetch the records, why not use use
#delegate
? It works just fine, at least in the scenario you've described.Although in my not-so-humble-opinion you should just chain the methods because it's more straightforward, and you're no longer achieving the performance boost unless you go with some crazy custom SQL as tadman says.
虽然它是一个非常有用的东西,但你不能 has_many :through a has_many :through 关系。这是连接引擎的限制。
替代方案是使用巧妙的子选择,或者在这种情况下使用子子选择,或者故意对表进行非规范化以减少连接深度。
例如,由于 Business 是在 Provider 的上下文中定义的,因此任何 Bid 元素也都会间接分配给 Provider。在出价和提供商之间建立直接关联将使直接查询出价变得容易。
Although it is a very useful thing to have, you can't has_many :through a has_many :through relationship. This is a limitation of the join engine.
The alternatives are either to use a clever sub-select, or in this case a sub-sub select, or to deliberately denormalize the tables enough to reduce the join depth.
For example, since a Business is defined within the context of a Provider, it stands to reason that any Bid elements are also assigned, indirectly, to a Provider. Building a direct association between Bid and Provider would make querying bids directly easy.
没有什么可以阻止你做这样的事情,据我所知:
然后调用 @user.bids 应该会产生所需的结果,如果你愿意,你还可以缓存出价并做其他奇特的事情。
There's nothing stopping you doing something like this afaik:
Then calling @user.bids should produce the desired result, you can also cache the bids and do other fancy stuff if you want.