Is it possible to make double has_many: through union in rails?

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 now I can:

 ab abfirst.d ad 

the first two commands are currently working, but on the third I get an SQL error. he seems to be trying to go straight through b in order to get d and not lift the fact that a → b goes through c.

How to solve?

+4
source share
2 answers

I don't think you can directly call ad , but you can do abmap(&:d).flatten to get all d from a

+5
source

A quick update for anyone who encounters this is possible after Rails 3.1: http://guides.rubyonrails.org/3_1_release_notes.html

In your example, here is how it would look:

 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!

+11
source

Source: https://habr.com/ru/post/1342825/


All Articles