I have four models: a, b, c, d
here is what I want to do:
a has_many b, :through => c
a has_many d, :through => b
so that in the console I can then do:
a.b
a.b.first.d
a.d
currently the first two commands work but on the third I get an SQL error. it appears to be trying to go straight through b to get d and not picking up the fact that a -> b goes through c.
How to solve?
Quick update on this for anyone that comes across it, this is possible after Rails 3.1: http://guides.rubyonrails.org/3_1_release_notes.html
In your example, here's how it would look like:
Class A
has_many :c
has_many :b, :through => :c, :source => :b
has_many :d, :through => :b, :source => :d
end
Class C
has_many :b
has_many :d, :through => :b, :source => :d
end
Class B
has_many :d
end
Class D
end
Just to clarify!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With