# Need help with deciding on the association

**URL:** <https://www.ruby-forum.com/t/need-help-with-deciding-on-the-association/118283>\
**Category:** Rails\
**Created:** [October 25, 2007, 11:54am UTC](https://www.ruby-forum.com/t/need-help-with-deciding-on-the-association/118283 "2007-10-25T11:54:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 25, 2007, 11:54am UTC](https://www.ruby-forum.com/t/need-help-with-deciding-on-the-association/118283/1 "2007-10-25T11:54:11Z")

</div>

I have a user model which has a users table associated with it.  
I need to create a relational database around the user table like a user  
can have many credit cards, a user can have many schemes, a user can  
know many languages so on and so forth. I have some 7 to 10 association  
between the user and different models.

How should I go about it?

1. Should I create a user table, a credit\_card table and a  
users\_credit\_cards table and have them in a has\_many through  
relationship. This way I would be creating a reference table for each of  
the association that is required. I would end up referencing some 15  
tables to render a single form.
2. Should I have them in a polymorphic association where I have a  
user\_details table which has a detail\_type which can contain the class  
name of either language,credit card etc.

Or do you think that there is a better way to do this task.

Any help in this would be highly appreciated.  
Thanks in advance:)  
Cheers,  
Jazzy

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 27, 2007, 3:41am UTC](https://www.ruby-forum.com/t/need-help-with-deciding-on-the-association/118283/2 "2007-10-27T03:41:37Z")

</div>

jazzy jazzy wrote:

> I have a user model which has a users table associated with it.  
> I need to create a relational database around the user table like a user  
> can have many credit cards, a user can have many schemes, a user can  
> know many languages so on and so forth. I have some 7 to 10 association  
> between the user and different models.
> 
> How should I go about it?
> 
> 1. Should I create a user table, a credit\_card table and a  
> users\_credit\_cards table and have them in a has\_many through  
> relationship. This way I would be creating a reference table for each of  
> the association that is required. I would end up referencing some 15  
> tables to render a single form.
> 2. Should I have them in a polymorphic association where I have a  
> user\_details table which has a detail\_type which can contain the class  
> name of either language,credit card etc.
> 
> Or do you think that there is a better way to do this task.
> 
> Any help in this would be highly appreciated.  
> Thanks in advance:)  
> Cheers,  
> Jazzy

Regarding the credit cards association, I’d say that a “has\_many  
:through” is probably the best way to go. Say a :user has\_many  
:credit\_cards, :through =\> :account or something.

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 27, 2007, 4:30am UTC](https://www.ruby-forum.com/t/need-help-with-deciding-on-the-association/118283/3 "2007-10-27T04:30:20Z")

</div>

Every entity can have its own table. Then you can define the  
relationships between them. The use cases will drive the actual design  
decisions. You will be able to figure out whether habtm or has many  
through will satisfy your requirements. I don’t think STI is a good  
solution for your scenario.
