Search code examples
ruby-on-railshas-manysingle-table-inheritancebelongs-to

Relation between two single-table inheritance models


I have the following two models

class ContactField < ActiveRecord::Base
end

class Address < ContactField
end

class Phone < ContactField
end

and

class Contact < ActiveRecord::Base
end

class Company < Contact
end

class Person < Contact
end

I want one contact, no matter is it Company or Person, to have many ContactFields(Addresses and Phones)... So where should I put those has many and belongs to? Thanks


Solution

  • You already said it in plain english :-)

    I want one contact, no matter is it Company or Person, to have many ContactFields(Addresses and Phones)... So where should I put those has many and belongs to? Thanks

    
    class Contact < ActiveRecord::Base
     has_many :contact_fields
    end
    
    class ContactField < ActiveRecord::Base
     belongs_to :contact
    end
    

    This Relationship will be inherited by both address and phone