# Has\_many\_through inheritance

**URL:** <https://www.ruby-forum.com/t/has-many-through-inheritance/171162>\
**Category:** Rails\
**Created:** [June 27, 2009, 9:05am UTC](https://www.ruby-forum.com/t/has-many-through-inheritance/171162 "2009-06-27T09:05:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben-v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecb155/32.png) [@ben-v](https://www.ruby-forum.com/u/ben-v)\
**Post date:** [June 27, 2009, 9:05am UTC](https://www.ruby-forum.com/t/has-many-through-inheritance/171162/1 "2009-06-27T09:05:15Z")

</div>

Hi all!

In my application I’m trying to create a model (Groups), which will  
allow me to save groups of objects from various tables under a common  
name. For example, one record in this table could refer to a group of  
users. Another could refer to a group of photos. How should I do this?  
When I use has\_many\_through I have to specify the group\_id as well as  
the id for whatever object I am referring to. But if the other id could  
refer to a photo, or a user, or another model, how do I setup this kind  
of inheritance? Do I have multiple foreign key fields in the table that  
links the two together? EX: group\_id, user\_id, photo\_id etc. and leave  
the fields not being used blank. Or does this cause an error because  
they are indexes?

Best,  
Ben

---

<div class="post-metadata">

**Author:** ![ben-v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecb155/32.png) [@ben-v](https://www.ruby-forum.com/u/ben-v)\
**Post date:** [June 27, 2009, 10:36am UTC](https://www.ruby-forum.com/t/has-many-through-inheritance/171162/2 "2009-06-27T10:36:18Z")

</div>

Ben V. wrote:

> Hi all!
> 
> In my application I’m trying to create a model (Groups), which will  
> allow me to save groups of objects from various tables under a common  
> name. For example, one record in this table could refer to a group of  
> users. Another could refer to a group of photos. How should I do this?  
> When I use has\_many\_through I have to specify the group\_id as well as  
> the id for whatever object I am referring to. But if the other id could  
> refer to a photo, or a user, or another model, how do I setup this kind  
> of inheritance? Do I have multiple foreign key fields in the table that  
> links the two together? EX: group\_id, user\_id, photo\_id etc. and leave  
> the fields not being used blank. Or does this cause an error because  
> they are indexes?
> 
> Best,  
> Ben

1. You should name the model like Group, not Groups
2. You should look for :polymorphic =\> true, somewhere like this:  
[http://railscasts.com/episodes/154-polymorphic-association](http://railscasts.com/episodes/154-polymorphic-association)  
[Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations)

or just type in Google “polymorphic association Rails”, like this  
[http://www.google.com.ua/search?hl=uk&client=firefox-a&rls=org.mozilla:en-US:official&hs=8kT&ei=z9lFSsb0MMG1sgby4MEM&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=polymorphic+association+Rails&spell=1](http://www.google.com.ua/search?hl=uk&client=firefox-a&rls=org.mozilla:en-US:official&hs=8kT&ei=z9lFSsb0MMG1sgby4MEM&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=polymorphic+association+Rails&spell=1)
