# Store data in join table

**URL:** <https://www.ruby-forum.com/t/store-data-in-join-table/145642>\
**Category:** Ruby\
**Created:** [August 12, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/store-data-in-join-table/145642 "2008-08-12T13:02:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ganeshk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7cd45c/32.png) [@ganeshk](https://www.ruby-forum.com/u/ganeshk)\
**Post date:** [August 12, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/store-data-in-join-table/145642/1 "2008-08-12T13:02:38Z")

</div>

Hi,  
I have one join table called

for example “students\_subjects” and i am having relationship like  
in  
student model  
:has\_many=\>subjects ,:through=\>students\_departments

in  
subjects table  
:has\_many=\>students ,:through=\>students\_departments

i need to store data in join table  
join table columns

‘id’ ‘student\_id’ ‘subject\_id’

how can i store plz reply if anybody knows solution…

---

<div class="post-metadata">

**Author:** ![ganeshk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7cd45c/32.png) [@ganeshk](https://www.ruby-forum.com/u/ganeshk)\
**Post date:** [August 12, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/store-data-in-join-table/145642/2 "2008-08-12T14:00:07Z")

</div>

On Tue, Aug 12, 2008 at 7:00 AM, Ganesh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> :has\_many=\>students ,:through=\>students\_departments
> 
> i need to store data in join table  
> join table columns
> 
> ‘id’ ‘student\_id’ ‘subject\_id’
> 
> how can i store plz reply if anybody knows solution…

You want the RubyOnRails mailing-list.

Anyways, just add the column to your join table, you’re already using  
has\_many :through, which is intended for join tables with additional  
data on the relationship.

Michael G.

---

<div class="post-metadata">

**Author:** ![ganeshk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7cd45c/32.png) [@ganeshk](https://www.ruby-forum.com/u/ganeshk)\
**Post date:** [August 12, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/store-data-in-join-table/145642/3 "2008-08-12T18:50:52Z")

</div>

On Tue, Aug 12, 2008 at 6:00 AM, Ganesh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> :has\_many=\>students ,:through=\>students\_departments
> 
> i need to store data in join table  
> join table columns
> 
> ‘id’ ‘student\_id’ ‘subject\_id’
> 
> how can i store plz reply if anybody knows solution…

I don’t know why people pick those type of names for tables, but a  
similar schema is…

create table people (  
account varchar not null primary key  
);

create table collection (  
team varchar not null primary key  
);

create table membership (  
account varchar not null references people (account),  
team varchar not null references collection (team),  
primary key (account, team)  
);

You’d want to add check constraints in a real database, along with  
other info, of course.

I’m pretty sure ActiveRecord will force you to use an arbitrary ID  
field with the join table. I haven’t used ActiveRecord in a while,  
but IIRC, I think I circumvented that annoyance by using “unique”  
instead of “primary key” for my multiple keys in join tables. The  
ActiveRecord::Base class does allow you to define your own primary  
keys (unless they’re multiple).

hth,  
Todd

---

<div class="post-metadata">

**Author:** ![ganeshk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7cd45c/32.png) [@ganeshk](https://www.ruby-forum.com/u/ganeshk)\
**Post date:** [August 12, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/store-data-in-join-table/145642/4 "2008-08-12T20:24:06Z")

</div>

On Tue, Aug 12, 2008 at 11:50 AM, Todd B.

> I don’t know why people pick those type of names for tables

By that I meant the tablea\_tableb nomenclature, which seems to be what  
all the business graduates are learning nowadays.

I would suggest using collective nouns when possible, followed by  
plural nouns if not.

My favorite database faux pas is the use of numbers in table names.

Rant over…

Todd
