我知道 Rails 不支持嵌套的 has_many :through 关系,尽管早在 Rails 2 中就已经有关于补丁的讨论和开放票证。
我确实来了 跨插件 非常流畅,但是 master 分支不能与 Rails 3 一起使用,我犹豫是否将它用于应用程序中的关键任务,因此缺乏活跃的近期任务发展。那么——处理这些关系的最佳方式是什么?
class Author < ActiveRecord::Base
has_many :contracts
has_many :products, :through => :contracts
class Product < ActiveRecord::Base
has_many :contracts
has_many :orders
has_many :authors, :through => :contracts
class Contracts < ActiveRecord::Base
belongs_to :author
belongs_to :product
因此,如果能够通过将其添加到作者模型中来获得命令,那就太棒了:
has_many :orders, :through => :products
但是,唉,你不能——至少没有插件。所以,我的问题是,当唯一的关联存在于连接模型和合同之间时,访问所有作者订单的最佳方法是什么?
I know Rails doesn't support nested has_many :through relationships, though there's been talk and an open ticket about a patch since as early as Rails 2.
I did come across a plugin that's pretty slick, but the master branches don't work with Rails 3 and I'm hesitant to use it for mission critical tasks in the app hence the lack of active recent development. So -- what's the best way to deal with these relations.
class Author < ActiveRecord::Base
has_many :contracts
has_many :products, :through => :contracts
class Product < ActiveRecord::Base
has_many :contracts
has_many :orders
has_many :authors, :through => :contracts
class Contracts < ActiveRecord::Base
belongs_to :author
belongs_to :product
So, all the being what it is it would be great to be able to get at orders this by adding this to the Author model:
has_many :orders, :through => :products
But alas, you cannot -- at least without the plugin. So, my question is what's the best approach to accessing all of an author's orders when the only association is between the join model, Contracts?
发布评论
评论(3)
如果您不尝试通过嵌套关联创建对象,而只想将其用于查找,那么 Rails 3 中的作用域是实现此目的的好方法。或者,您可以实现一个类方法。
我最近在教的课上就遇到过这样的事情,Rails 3版本的代码在这里:
https://github.com/wolframarnold/Efficient -TDD-Rails3/blob/master/app/models/user.rb
查看 items 方法的定义。规格在这里:
https://github.com/wolframarnold/Efficient -TDD-Rails3/blob/master/spec/models/user_orders_spec.rb
Rails 3.1 更新:正如一位评论者已经指出的那样,Rails 3.1 确实支持 has_many :通过超过一层深度的关联。
If you're not trying to create objects through the nested association, and you want to use it for lookup only, then scopes in Rails 3 are a great way to do this. Alternatively you could implement a class method.
I've had this kind of thing as an example in a class I taught recently, the Rails 3 version of the code is here:
https://github.com/wolframarnold/Efficient-TDD-Rails3/blob/master/app/models/user.rb
See the definition of the items method. The specs are here:
https://github.com/wolframarnold/Efficient-TDD-Rails3/blob/master/spec/models/user_orders_spec.rb
Rails 3.1 update: As one commenter already noted, Rails 3.1 does support has_many :through associations more than one level deep.
在我看来,您有 2 个选择:
您可能需要重新考虑您的建模决策。例如,通过订单在客户和产品之间建立多对多关联。然后孵化合约。
使用命名范围(或 Rails 3 中的范围)来获取作者的订单。
如果有压力的话,我会选择选项 1。
As I see it you have 2 options:
You may need to re-consider your modelling decisions. For example, establish a many-to-many association between a Customer and a Product through an Order. And then incubate the Contract.
Use a named scope (or scope in Rails 3) to fetch the author's orders.
If pressed, I would go with option 1.
由于将补丁包含在 Rails 核心中,该票证似乎不再有效。我会提交它......看起来它应该是只在 Rails 中工作的东西。
The ticket doesn't seem to be active any more for including the patch in Rails core. I'd submit it ... seems like it should be something that should just work in Rails.