# Defining Relationship ( Many to Many) for 3 Models in Rails

**URL:** <https://www.ruby-forum.com/t/defining-relationship-many-to-many-for-3-models-in-rails/227713>\
**Category:** Rails\
**Created:** [March 26, 2013, 9:30am UTC](https://www.ruby-forum.com/t/defining-relationship-many-to-many-for-3-models-in-rails/227713 "2013-03-26T09:30:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Myth17](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Myth17](https://www.ruby-forum.com/u/Myth17)\
**Post date:** [March 26, 2013, 9:30am UTC](https://www.ruby-forum.com/t/defining-relationship-many-to-many-for-3-models-in-rails/227713/1 "2013-03-26T09:30:16Z")

</div>

I have three models in Rails as : Curriculum, Grade and Topics.

The relationship scenario is like :

Curriculum ‘International Baccalaureate(IB)’ in grade ‘6’ has\_many  
Topics (  
t1,t2,t3 and CompareFraction)

Curriculum ‘CBSE’ in grade ‘5’ has\_many Topics (t1,t2,t4,t5 and  
CompareFraction) [ A Topic ‘Compare Fraction’ will be taught in many  
different curriculums but maybe in different grades ]

A Grade, say 5 will itself be a part of all Curriculums like IB,CBSE.

I need to store information such that for a Topic Compare Fraction, I  
can  
say:

It is taught in IB in grade 5

It is taught in CBSE in grade 4.

How can I set this up in Rails?

---

<div class="post-metadata">

**Author:** ![Myth17](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Myth17](https://www.ruby-forum.com/u/Myth17)\
**Post date:** [March 26, 2013, 10:17am UTC](https://www.ruby-forum.com/t/defining-relationship-many-to-many-for-3-models-in-rails/227713/2 "2013-03-26T10:17:00Z")

</div>

On 26 March 2013 08:29, Myth17 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A Grade, say 5 will itself be a part of all Curriculums like IB,CBSE.
> 
> I need to store information such that for a Topic Compare Fraction, I can  
> say:
> 
> It is taught in IB in grade 5
> 
> It is taught in CBSE in grade 4.
> 
> How can I set this up in Rails?

You have not given us enough information.

I have no idea what a Grade object is. What are the fields of a grade  
object?

Colin

---

<div class="post-metadata">

**Author:** ![Myth17](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Myth17](https://www.ruby-forum.com/u/Myth17)\
**Post date:** [March 26, 2013, 10:35am UTC](https://www.ruby-forum.com/t/defining-relationship-many-to-many-for-3-models-in-rails/227713/3 "2013-03-26T10:35:07Z")

</div>

Grade is anything like 4,5 or say HighSchool with a ‘name’ and  
‘description’ attribute.

---

<div class="post-metadata">

**Author:** ![Myth17](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Myth17](https://www.ruby-forum.com/u/Myth17)\
**Post date:** [March 26, 2013, 10:45am UTC](https://www.ruby-forum.com/t/defining-relationship-many-to-many-for-3-models-in-rails/227713/4 "2013-03-26T10:45:03Z")

</div>

On 26 March 2013 09:33, Myth17 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Grade is anything like 4,5 or say HighSchool with a ‘name’ and ‘description’  
> attribute.

Please don’t top post, it makes it difficult to follow the thread.  
Thanks.

So a curriculum has many grades and a grade has many curriculums.  
A topic is associated with a set of grade/curriculum combinations.

I think the solution is a join table (you will have to think of a good  
name for it), I will say c\_g\_ts to save typing here. So:  
Curriculum has\_many c\_g\_ts and has\_many topics through c\_g\_t and  
has\_many grades through c\_g\_t  
Grade has\_many c\_g\_ts and has\_many curriculums through c\_g\_t and  
has\_many topics through c\_g\_t  
Topic has\_many …

CGT belongs\_to Curriculum and Grade and Topic.

Does that sound as if it will do the job?

Colin

---

<div class="post-metadata">

**Author:** ![Myth17](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Myth17](https://www.ruby-forum.com/u/Myth17)\
**Post date:** [March 26, 2013, 11:37am UTC](https://www.ruby-forum.com/t/defining-relationship-many-to-many-for-3-models-in-rails/227713/5 "2013-03-26T11:37:37Z")

</div>

On Tuesday, March 26, 2013 3:13:05 PM UTC+5:30, Colin L. wrote:

> Does that sound as if it will do the job?
> 
> > > > The relationship scenario is like :
> > > 
> > > You have not given us enough information.
> > > 
> > > I have no idea what a Grade object is. What are the fields of a grade  
> > > object?
> > > 
> > > Colin

Apologies for Top Posting.  
The solution looks great. Thanks Colin! I was aware of has\_many through  
but  
did not know I could apply it here for three models.
