Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:has_many :through associations two levels deep

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

I'm trying to get an association from Project has_many :invoices, :through => :tasks

When I tried that, I get:

Invalid source reflection macro :has_many :through for has_many :invoices, :through => :tasks. Use :source to specify the source reflection.

I'm a little at a loss to if this is possible, and if so, how to use :source correctly

like image 236
oreoshake Avatar asked Mar 09 '11 01:03

oreoshake


2 Answers

Ryan is right, this is supported from Rails 3.1. Extracted from the release notes:

Associations with a :through option can now use any association as the through or source association, including other associations which have a :through option and has_and_belongs_to_many associations.

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

like image 188
gamov Avatar answered Oct 18 '22 02:10

gamov


You can't do this in Rails 3.0. In Rails 3.1 I think this will be possible.

Instead, you have to use the nested_has_many_through plugin: http://github.com/ianwhite/nested_has_many_through

like image 20
Ryan Bigg Avatar answered Oct 18 '22 04:10

Ryan Bigg