Topic: When to use has_one

Hi there,

I'm a rails newbie here, and have spent a while reading documents.  However, I'm having a hard time figuring this out.

Table Company
- has one CEO which is a contact
- has one accountant which is a contact

Contact
- name
- role which is an enum of {:Accountant, :CEO}

How would one model this using has_one, or does that not even make sense?  It would be nice to do company.accountant.name, etc.

Thank you!

Thean.

Re: When to use has_one

try that:

class Company < ActiveRecord::Base
# the companies table nedds following columns: id, :company_id, name
has_many :contacts
has_one :ceo, :class_name => 'Contact', :conditions => "contacts.role = 'CEO'"
has_one :accountant, :class_name => 'Contact', :conditions => "contacts.role = 'Accountant'"
end

class Contact < ActiveRecord::Base
# the contacts table nedds following columns: id, :company_id, name
belongs_to :company
end


cheers,
maze