【问题标题】::has_many :through associations two levels deep:has_many :通过关联两层深
【发布时间】:2011-10-21 18:24:06
【问题描述】:
Project
has_many :tasks

Task
has_many :invoices, :through => :line_items
has_many :line_items

LineItems
belongs_to :invoice
belongs_to :task

Invoice
has_many :tasks, :through=> :line_item
has_many :line_items

我正在尝试从 Project has_many :invoices, :through => :tasks 中获取关联

当我尝试这样做时,我得到:

无效的源反射宏 :has_many :through for has_many :invoices, :through => :tasks。使用 :source 指定源反射。

我有点不知所措,如果这是可能的,如果可以,如何正确使用 :source

【问题讨论】:

    标签: ruby-on-rails-3 activerecord


    【解决方案1】:

    Ryan 是对的,Rails 3.1 支持这一点。摘自发行说明:

    带有 :through 选项的关联现在可以使用任何关联作为 through 或 source 关联,包括具有 :through 选项和 has_and_belongs_to_many 关联的其他关联。

    源:http://guides.rubyonrails.org/3_1_release_notes.html

    【讨论】:

      【解决方案2】:

      在 Rails 3.0 中你不能这样做。在 Rails 3.1 中,我认为这是可能的。

      相反,您必须使用nested_has_many_through 插件:http://github.com/ianwhite/nested_has_many_through

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        相关资源
        最近更新 更多