Stephen Gerstacker
2006-Jan-04  23:03 UTC
[Rails] Multiple Associations With The Same Object
I have a Company object which has multiple Contacts 
(has_many/belongs_to)  I would like one of the contacts to be the 
primary contact.  What''s the best way to do this?  Right now I have:
create table companies (
 id int not null auto_increment,
 name varchar(50) not null,
 primary_id int null,
 constraint fk_company_primary_contact foreign key (primary_id) 
references contacts(id),
 primary key (id)
);
create table contacts (
 id int not null auto_increment,
 name varchar(100) not null,
 company_id int not null,
 primary key (id)
);
class Company < ActiveRecord::Base
  has_many :contacts
  has_one :primary_contact,
          :class_name => ''Contact''
  ...
end
class
class Contact < ActiveRecord::Base
  belongs_to :company
  ...
end
Can someone help me?
-- 
Posted via http://www.ruby-forum.com/.
I''ve not tried it, but I''d think you could do something like this: class Company < ActiveRecord::Base has_many :contacts has_one :primary_contact ... end class Contact < ActiveRecord::Base belongs_to :company ... end class PrimaryContact < Contact end Although you may have to add a "where clause" to the PrimaryContact class to define how to identify the primary contact.