# Has\_many: through, :class\_name, :foreign\_key

**URL:** <https://www.ruby-forum.com/t/has-many-through-class-name-foreign-key/130561>\
**Category:** Rails\
**Created:** [February 29, 2008, 11:53am UTC](https://www.ruby-forum.com/t/has-many-through-class-name-foreign-key/130561 "2008-02-29T11:53:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan\_Ukhov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@Ivan\_Ukhov](https://www.ruby-forum.com/u/Ivan_Ukhov)\
**Post date:** [February 29, 2008, 11:53am UTC](https://www.ruby-forum.com/t/has-many-through-class-name-foreign-key/130561/1 "2008-02-29T11:53:29Z")

</div>

Hello!

When we use has\_many :through, is there a way to specify the name of  
the class and the foreign key? This question pops up when the join  
table has fields that are not named according to names of the joinable  
tables.

Thanks.

---

<div class="post-metadata">

**Author:** ![Ivan\_Ukhov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@Ivan\_Ukhov](https://www.ruby-forum.com/u/Ivan_Ukhov)\
**Post date:** [February 29, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/has-many-through-class-name-foreign-key/130561/2 "2008-02-29T16:20:43Z")

</div>

Ivan Ukhov wrote:

> Hello!
> 
> When we use has\_many :through, is there a way to specify the name of  
> the class and the foreign key? This question pops up when the join  
> table has fields that are not named according to names of the joinable  
> tables.
> 
> Thanks.

Hey,

Say you have two entities A and B which need to have m-\>n relationship  
in the ERM,  
so ideally they would have a join table A\_B\_join\_table with the foreign  
keys from both A and B.

Now, say sometime later you decide that the join table should also  
contain some additional data that would help your business logic. That’s  
the time when you should make the join table an active record model,say  
C. But it should not break the relationship between A and B too.

That’s when you can use A has\_many B through C where C is also an active  
record model. Result A → C → B similar to A → B as earlier.

Also, in Rails 2.0, that uses RESTful architecture, it is emphasized to  
use active record models instead of stand-alone join tables.

Check out the RailsConf Keynote by DHH. Make sure you have downloaded  
the pdf before watching the video.

> **[RailsConf Keynote: David Heinemeier Hansson — ScribeMedia](https://www.scribemedia.org/dhh/)**
>
> We independently review everything we recommend. When you buy through our links, we may earn a commission. Read the full disclosure here. With the release of Rails 1.2, we take a look back at David Heinemeier Hansson’s RailsConf keynote from Chicago....
