Rails has_many:通过 has_many:通过

发布于 2024-08-17 14:27:03 字数 609 浏览 2 评论 0原文

我想知道我可以在 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.businessesProvider.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

命硬 2024-08-24 14:27:03

这是完全可能的,但需要一些额外的工作。以下模型定义与 nested_has_many 插件 结合使用,您只需使用 < code>@user.bids

class User < ActiveRecord::Base
    has_one :provider
    has_many :businesses, :through => :provider
    has_many :bids, :through => :businesses
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

然而,从出价中获取用户需要更多的工作。

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

class User < ActiveRecord::Base
    has_one :provider
    has_many :businesses, :through => :provider
    has_many :bids, :through => :businesses
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

However getting a user from a bid will take a more work.

长途伴 2024-08-24 14:27:03

如果您只想获取记录,为什么不使用 #delegate?它工作得很好,至少在你描述的场景中是这样。

class User < ActiveRecord::Base
    has_one :provider
    delegate :bids, :to => :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

尽管在我不那么谦虚的观点中,你应该只链接这些方法,因为它更简单,并且除非你像 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.

class User < ActiveRecord::Base
    has_one :provider
    delegate :bids, :to => :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

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.

猥︴琐丶欲为 2024-08-24 14:27:03

虽然它是一个非常有用的东西,但你不能 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.

一抹淡然 2024-08-24 14:27:03

没有什么可以阻止你做这样的事情,据我所知:

class User < ActiveRecord::Base
    has_one :provider
    has_many :businesses, :through => :provider

    def bids
        user_bids = []
        businesses.each |business| do
            user_bids += business.bids
        end
        user_bids
    end
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.bids 应该会产生所需的结果,如果你愿意,你还可以缓存出价并做其他奇特的事情。

There's nothing stopping you doing something like this afaik:

class User < ActiveRecord::Base
    has_one :provider
    has_many :businesses, :through => :provider

    def bids
        user_bids = []
        businesses.each |business| do
            user_bids += business.bids
        end
        user_bids
    end
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

Then calling @user.bids should produce the desired result, you can also cache the bids and do other fancy stuff if you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文