# New to rails - question about :through and habtm - example

**URL:** <https://www.ruby-forum.com/t/new-to-rails-question-about-through-and-habtm-example/112350>\
**Category:** Rails\
**Created:** [August 17, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/new-to-rails-question-about-through-and-habtm-example/112350 "2007-08-17T19:54:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sthorpe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@sthorpe](https://www.ruby-forum.com/u/sthorpe)\
**Post date:** [August 17, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/new-to-rails-question-about-through-and-habtm-example/112350/1 "2007-08-17T19:54:41Z")

</div>

I’ve been designing databases for a few years now, but I’m just recently  
starting a project, and rails seems like a good candidate to use for it.  
I think I might be confused about how rails deals with associations,  
this is the situation:

I have a table of people, roles, and classes (also people\_roles,  
people\_roles\_classes?). People can have many roles, and a particular  
role can be associated with many people. That seems like a standard  
HABTM relationship. But then we throw in the classes: an instance of  
people & roles (people\_roles) will be associated with many different  
classes as different roles (teacher/student), so would the models look  
like this?

class People \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :roles  
end

class Role \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :people  
end

class People\_Role \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :classes  
end

class Classes  
has\_and\_belongs\_to\_many :people\_roles  
end

I think I just confused myself even more, but is this almost correct?

---

<div class="post-metadata">

**Author:** ![sthorpe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@sthorpe](https://www.ruby-forum.com/u/sthorpe)\
**Post date:** [August 17, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/new-to-rails-question-about-through-and-habtm-example/112350/2 "2007-08-17T21:55:38Z")

</div>

Hey Scott,

I’m new to Rails myself, but hope I can help in some way…

Perhaps a simplification to your model might help. Would it possible  
to consider it this way instead:

A person may attend many classes. Additionally, a class may be  
attended by many people. When a person attends a class, they are  
associated a role.

This simplification prohibits a person from having more than one role  
for a particular class. For example, Alan can attend Chemistry as a  
student and English as a teacher. But Alan could not attend Chemistry  
as a student AND a teacher.

Let me know if this simplification seems viable; if so, I’ll see if I  
can help you build your models 🙂

Hope this helps.

Cheers,  
Louis.

---

<div class="post-metadata">

**Author:** ![sthorpe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@sthorpe](https://www.ruby-forum.com/u/sthorpe)\
**Post date:** [August 17, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/new-to-rails-question-about-through-and-habtm-example/112350/3 "2007-08-17T23:47:51Z")

</div>

Hey Louis,

I’ve been looking about the design this afternoon, and it looks like it  
would be easier to explicitly spell out the roles (student/teacher/etc)  
into tables with polymorphic associations. The previous design was from  
more of a high level to prevent redundancy of data between roles; a  
little won’t hurt.

As to preventing a single person from being duplicated in a class, did  
you plan on attempting this through the sql by making the id’s unique?  
Or is there an easier way in the model (.rb) itself?

Thanks for your help!

– Scott

removed\_email\_address@domain.invalid wrote:

> Hey Scott,
> 
> I’m new to Rails myself, but hope I can help in some way…
> 
> Perhaps a simplification to your model might help. Would it possible  
> to consider it this way instead:
> 
> A person may attend many classes. Additionally, a class may be  
> attended by many people. When a person attends a class, they are  
> associated a role.
> 
> This simplification prohibits a person from having more than one role  
> for a particular class. For example, Alan can attend Chemistry as a  
> student and English as a teacher. But Alan could not attend Chemistry  
> as a student AND a teacher.
> 
> Let me know if this simplification seems viable; if so, I’ll see if I  
> can help you build your models 🙂
> 
> Hope this helps.
> 
> Cheers,  
> Louis.

---

<div class="post-metadata">

**Author:** ![sthorpe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@sthorpe](https://www.ruby-forum.com/u/sthorpe)\
**Post date:** [August 21, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/new-to-rails-question-about-through-and-habtm-example/112350/4 "2007-08-21T14:41:17Z")

</div>

I ran into a similar situation recently. The best I could come up with  
was remodelling the structure (as everyone else in this thread have  
said) instead of trying to hack around to get it working as it was.  
But it could be possible to come up with something by redefining  
classes and maybe writing ugly SQL yourself, although I do not believe  
that it is the ideal way to do it as far as scalability and (later)  
readability is concerned.

---

<div class="post-metadata">

**Author:** ![sthorpe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@sthorpe](https://www.ruby-forum.com/u/sthorpe)\
**Post date:** [August 18, 2007, 10:22am UTC](https://www.ruby-forum.com/t/new-to-rails-question-about-through-and-habtm-example/112350/5 "2007-08-18T10:22:32Z")

</div>

Hey Scott,

Great. I’m glad you’ve found a different approach that works!

Yes: I guess my thinking is if the IDs are unique, and the resolution  
table (people\_classes) contains a primary composed of the two foreign  
keys (person\_id, class\_id), then a single person could not be  
duplicated in a class (and vice-versa).

Good luck!

- Louis.

On Aug 17, 10:47 pm, Scott Tesoriere \<rails-mailing-l…@andreas-
