0

I have a model person and a model group. There are two kinds of persons: leaders lead a group and participants participate. I need a hbtm-relationship between leaders and groups and a has_many-relationship between participants and groups. Is it possible to do this with the same model, person by providing some kind of condition (is a leader/is a participant) in the model?

class Person < ActiveRecord::Base
  has_and_belongs_to_many :groups
  has_many :participations
  has_many :groups, :through => :participations
  ...
end

I would like to do this with one model, person, because users are either leaders or participants but each user should be a person, i.e. User belongs_to :person.

ewi
  • 175
  • 1
  • 9

1 Answers1

0

you should only one class for persons, not more. You can do something like:

class Person < ActiveRecord::Base
  has_many :relations
  has_many :groups, :through => :relations
   ...
end

class Group < ActiveRecord::Base
  has_many :relations
  has_many :persons, :through => :relations
  ...
end

class Relation < ActiveRecord::Base
  belongs_to :person
  belongs_to :group
end

table 'relations' should have next to person_id and group_id, one more field, called, for example 'leader' and the value should be true/false or 1/0. So, if the person of the group is leader, the value should be 1/true, if not then 0/false

Marko Krstic
  • 1,417
  • 1
  • 11
  • 13