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

15 views
Skip to first unread message

Myth17

unread,
Mar 26, 2013, 4:29:12 AM3/26/13
to rubyonra...@googlegroups.com

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?

Colin Law

unread,
Mar 26, 2013, 5:15:16 AM3/26/13
to rubyonra...@googlegroups.com
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

Myth17

unread,
Mar 26, 2013, 5:33:39 AM3/26/13
to rubyonra...@googlegroups.com, cla...@googlemail.com
Grade is anything like 4,5 or say HighSchool with a 'name' and 'description' attribute. 

Colin Law

unread,
Mar 26, 2013, 5:43:05 AM3/26/13
to rubyonra...@googlegroups.com
On 26 March 2013 09:33, Myth17 <nitish...@gmail.com> 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

Myth17

unread,
Mar 26, 2013, 6:36:18 AM3/26/13
to rubyonra...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages