views:

78

answers:

4

I have two classes that I would like to specify as follows:

class Club < ActiveRecord::Base
  belongs_to :president, :class_name => "Person", :foreign_key => "president_id"
  belongs_to :vice_president, 
             :class_name => "Person",
             :foreign_key => "vice_president_id"
end

class Person < ActiveRecord::Base
  has_one :club, :conditions => 
  ['president_id = ? OR vice_president_id = ?', '#{self.id}', '#{self.id}']
end

This doesn't work and gives me an error when trying to get the club association from the person object. The error is because is looking for person_id in the club table when I looked at the SQL. I can get around it by declaring multiple has_one associations, but feel like this is the improper way of doing it.

A person can only be the President or Vice President of one club.

Anyone able to offer a little bit of advice on this issue, I would be very appreciative.

A: 

I think your associations are the wrong way around. Your way it is hard to assign a president or vice president.

I would do it like this:

class Club < ActiveRecord::Base
  has_one :president, 
          :class_name => "Person",
          :foreign_key => 'president_club_id'
  has_one :vice_president, 
          :class_name => "Person", 
          :foreign_key => 'vice_president_club_id'
end

class Person < ActiveRecord::Base
  belongs_to :club
end

Now you can assign the roles like this:

club.president = Person.create(:name => 'Tom')
club.vice_president = Person.create(:name => 'Andrew')
That wouldn't work though because then I would need to have a president_club_id and vice_president_club_id in the People table (and in my actual application have two more positions as well). Plus I would have to have multiple belongs_to in the Person class for that to work.Additionally some people may not be in charge of clubs and rather 'employees' so I would like to keep the foreign_key out of their model.
adimitri
A: 

I suggest you introduce a new model called role. Then have the following:

class Club
  has_many :roles 

  def president
  end

  def vice_president
  end

end

class Person
  belongs_to :role
end

class Role
  has_one :person
  belongs_to :club
end
Sohan
A: 

This is the classic use case for polymorphic associations. Here is the link: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Something like..

class Club < ActiveRecord::Base
  belongs_to :person, :polymorphic => true 

class Person < ActiveRecord::Base
  has_one :club, :as => :position
tsenart
Polymorphic associations would not help solve the problem here because its multiple people being accessed from the the same model. Polymorphic associations are for when your foreign_key may be different models.
adimitri
+2  A: 

Your has_one condition will never work in Rails, as far as I know.

You need one explicit has_one or belongs_to or has_many per "link", on both tables. So if you have two "links", you need two has_one and two belongs_to. That is how it works.

Secondly, I think you should reconsider your models. The way you are doing it, one person can not be the president of a club and an employee, at the same time. Or be the president of two clubs. Even if you don't have these right now, they can come in the future - it is easier to stay flexible right now.

A flexible way of doing this is using a has_many :through with an intermediate table that specifies the role. In other words:

# The memberships table has a person_id, club_id and role_id, all integers

class Membership < ActiveRecord::Base
  belongs_to :club
  belongs_to :person
  validates_presence_of :role_id
  validates_numericality_of :role_id
end

class Club < ActiveRecord::Base
  has_many :memberships, :dependent => :delete_all
  has_many :people, :through => :memberships
end

class Person < ActiveRecord::Base
  has_many :memberships, :dependent => :delete_all
  has_many :clubs, :through => :memberships
end

Now, assuming that role_id=0 means employee, role_id=1 means president, and role_id=2 means vice_president, you can use it like this:

tyler = Person.find(1) # person_id is 1
other = Person.find(2) # person_id is 2
c = Club.find(1)  # club_id is 1

tyler.clubs # returns all the clubs this person is "member" of
c.people # returns all the "members" of this club, no matter their role

#make tyler the president of c
tyler.memberships.create(:club_id => 1, :role_id => 1)

#make other the vicepresident of c
#but using c.memberships instead of other.memberships (works both ways)
c.memberships.create(:person_id => 2, :role_id => 1)

#find the (first) president of c
c.memberships.find_by_role_id(1).person

#find the (first) vicepresident of c
c.memberships.find_by_role_id(2).person

#find all the employees of c
c.memberships.find_all_by_role_id(0).collect { |m| m.person }

#find all the clubs of which tyler is president
tyler.memberships.find_all_by_role_id(1).collect { |m| m.club }

Additional notes:

  • You could complement this with a roles table and model. Roles would have just a a name, roles would have_many relationships and memberships would belong_to role. Or, you could define methods in memberships for getting the role name (if 0, it returns "employee", if 1, "president", etc
  • You can add validations on memberhips so no more than 1 person can be made president of a given club, or the same employee on the same club twice. Later on, if you start getting "exceptional cases" in which a person needs to be in two places, you will just have to adapt your validations.
egarcia
Ok so, this definitely seems like a better structure. However I'm not sure how the employee thing works into this. The employee designation is really independent if they are part of a club or not, so I don't think it should be part of the membership table. Incidentally, I already use a Roles model for the different employee roles we have. So maybe on the Person model there should be an association to a has_one EmployeeRole and that gives their employee type?
adimitri
Hi there! I'm not sure I understand what you mean by 'really independent if they are part of a club or not'. If you have a Roles table with employee roles, you could add 2 more("president" and "vice president") and just use one thing. Why do you want to have them separated?
egarcia
This application is for a student government organization at a college campus. They fund a lot of clubs who are run by students and have 4 e-board positions (President, VP, Secretary Treasurer). The employees are of the student government and also students but it has nothing to do with the club. So they may be part of a club e-board and an employee.
adimitri
I can't tell for sure, but it seems that what you are trying to do is separating "students" from "regular people". Maybe they have some fields in common(name, address) but not others(classroom)? In that case, I suggest that you have a look at Single Table Inheritance. Your students could be "subclasses" of Person.
egarcia