# Associations

**URL:** <https://www.ruby-forum.com/t/associations/165707>\
**Category:** Rails\
**Created:** [April 16, 2009, 4:36pm UTC](https://www.ruby-forum.com/t/associations/165707 "2009-04-16T16:36:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 16, 2009, 4:36pm UTC](https://www.ruby-forum.com/t/associations/165707/1 "2009-04-16T16:36:47Z")

</div>

I’d like to do something like this…in English, a Teacher is in a town  
(easy).

They also can have qualifications (from none to lots). How do I do this,  
I mean I can see a Teacher could have a QualificationID field which  
could have could contain numbers like 3,4,6 (meaning they have  
qualifications of X, Y and Z). How do I do it in rails associations?

Teacher (Belongs to a Location)

- FirstName
- LastName
- EmailAddress
- MobilePhoneNumber
- LocationID
- QualificationID(s) \<—

Location (Has Many Teachers)

- Name

Qualification \<— what to do here?

- Name

* * *

Ultimately I’d like to do things like Teacher.Location.name (easy).

I’d like to do Teacher.Qualifications also (Not sure how to express this  
but you get the idea).

Any help appreciated…

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 16, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/associations/165707/2 "2009-04-16T17:36:52Z")

</div>

On 16 Apr 2009, at 15:36, bingo bob wrote:

> I’d like to do something like this…in English, a Teacher is in a town  
> (easy).
> 
> They also can have qualifications (from none to lots). How do I do  
> this,  
> I mean I can see a Teacher could have a QualificationID field which  
> could have could contain numbers like 3,4,6 (meaning they have  
> qualifications of X, Y and Z). How do I do it in rails associations?

sounds like you need a has many through (or has and belongs to many)  
between teachers and qualifications, since I assume that many teachers  
may have the same qualification. This join model could also hold data  
like when the qualification was awarded etc.

Fred

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 16, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/associations/165707/3 "2009-04-16T18:00:35Z")

</div>

You can always add an assignment table like this:

class Teacher \< ActiveRecord::Base  
belongs\_to :location  
has\_many :qualifications

# location\_id

end

class Location \< ActiveRecord::Base  
has\_many :teachers  
end

class Qualification \< ActiveRecord::Base  
belongs\_to :quality  
belongs\_to :user

# teacher\_id, quality\_id

end

class Quality \< ActiveRecord::Base  
has\_many :qualifications  
end

User.location # New York  
User.qualifications # List of qualities

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [April 16, 2009, 6:01pm UTC](https://www.ruby-forum.com/t/associations/165707/4 "2009-04-16T18:01:48Z")

</div>

Er, sorry about that. Replace any instances of “user” you saw in there  
with “teacher”, lost my train of thought there for a bit.
