Reputation: 11421
I have a model called massmedia and this model belongs to company.
We have companies: PRIME, JURNAL, ASMEDIA, MEDIA GROUP
and massmedia: PRIME TV, JURNAL TV, JURNAL FM, PUBLIKA TV
and the relation:
Company TV Channel Company
1. **PRIME** owner of **PRIME TV**
2. **JURNAL** owner of **JURNAL TV**
3. **ASMEDIA** owner of **PUBLIKA TV** owner of **PRIME**
4. **MEDIA GROUP** owner of **PRIME**
Now I got into problem where company PRIME is owned by ASMEDIA and MEDIA GROUP and that means I have to relate company
to company
and I am not sure if this can be done, even creating a new table for relation between 2 companies it's not good as I would need a table with 2 columns company_id of owned company and company_id of the one hwo owns it.
Any ideas how can I solve this? Thank you.
EDIT
I tried to create a scheme of what I tried to accomplish:
MediaGrup is the owner of the company Prime and companies shareholder 1 and 2 are the owners of MediaGrup
so all of them: MediaGrup, Shareholder1 and 2 are in company model.
Upvotes: 0
Views: 61
Reputation: 24340
You can have relations between instances of the same classe. For example with has_one
and belongs_to
(it possible also with has_many
, but your example shows no 0..n association) :
class MassMedia< ActiveRecord::Base # the table "mass_medias" must have a column 'owner_id'
belongs_to :owner, :class_name => 'Company', :foreign_key => 'owner_id', :inverse_of => :mass_media
end
class Company < ActiveRecord::Base # the table "companies" must have a column 'owner_id'
has_one :mass_media, :inverse_of => :owner
has_one :company, :inverse_of => :owner
belongs_to :owner, :class_name => 'Company', :foreign_key => 'owner_id', :inverse_of => :company
end
Upvotes: 1