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?
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.
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
[email protected] 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.
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.
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!
On Aug 17, 10:47 pm, Scott Tesoriere <rails-mailing-l…@andreas-