Forum: Ruby on Rails associations...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
bingo b. (Guest)
on 2009-04-16 18:36
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....
Frederick C. (Guest)
on 2009-04-16 19:36
(Received via mailing list)
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
Jack B. (Guest)
on 2009-04-16 20:00
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
Jack B. (Guest)
on 2009-04-16 20:01
Er, sorry about that. Replace any instances of "user" you saw in there
with "teacher", lost my train of thought there for a bit.
This topic is locked and can not be replied to.