# Has\_many :through with has\_many/has\_many join models

**URL:** <https://www.ruby-forum.com/t/has-many-through-with-has-many-has-many-join-models/94107>\
**Category:** Rails\
**Created:** [April 21, 2006, 12:57am UTC](https://www.ruby-forum.com/t/has-many-through-with-has-many-has-many-join-models/94107 "2006-04-21T00:57:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [April 21, 2006, 12:57am UTC](https://www.ruby-forum.com/t/has-many-through-with-has-many-has-many-join-models/94107/1 "2006-04-21T00:57:12Z")

</div>

It seems that using a join model that joins with two has\_many’s will  
fail to generate proper SQL

class StudentSemesterRecord \< ActiveRecord::Base  
belongs\_to :semester  
has\_many :discipline\_records, :through =\> :semester  
end

class Semester \< ActiveRecord::Base  
has\_many :student\_semester\_records  
has\_many :discipline\_records  
end

class DisciplineRecord \< ActiveRecord::Base  
belongs\_to :semester  
end

student\_semester\_record.discipline\_records yields this SQL:  
SELECT discipline\_records.\* FROM discipline\_records INNER JOIN semesters  
ON discipline\_records.semester\_id = semesters.id WHERE  
(semesters.semester\_id = 1349)

The where clause is invalid. It is using:  
semesters.semester\_id = #{student\_semester\_record.id}

It should be using:  
semesters.id = #{student\_semester\_record.semester\_id}

Am I doing something wrong, is this a bug, or is this association type  
simply not supported?

Thanks!

–  
Jack C.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [April 21, 2006, 1:15am UTC](https://www.ruby-forum.com/t/has-many-through-with-has-many-has-many-join-models/94107/2 "2006-04-21T01:15:32Z")

</div>

Jack C. wrote:

> It seems that using a join model that joins with two has\_many’s will  
> fail to generate proper SQL
> 
> class StudentSemesterRecord \< ActiveRecord::Base  
> belongs\_to :semester  
> has\_many :discipline\_records, :through =\> :semester  
> end
> 
> class Semester \< ActiveRecord::Base  
> has\_many :student\_semester\_records  
> has\_many :discipline\_records  
> end
> 
> class DisciplineRecord \< ActiveRecord::Base  
> belongs\_to :semester  
> end

You can do a has\_many :through two ways. One is using a join model with  
two belongs\_to associations. The other is to go through a model with a  
belongs\_to and a has\_many association. The setup you describe is  
interesting, but not currently supported.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [April 21, 2006, 1:36am UTC](https://www.ruby-forum.com/t/has-many-through-with-has-many-has-many-join-models/94107/3 "2006-04-21T01:36:53Z")

</div>

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [April 21, 2006, 3:16am UTC](https://www.ruby-forum.com/t/has-many-through-with-has-many-has-many-join-models/94107/4 "2006-04-21T03:16:22Z")

</div>

Jack C. wrote:

> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

listserv seems to have eaten your reply

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
