Associations


#1

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…


#2

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


#3

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


#4

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