Im starting to learn rails today and not quite sure how this would work:
Lets say I have a Company, the company may have many subsidiaries.
A subsidiary is a Company. A company cannot be its own subsi diary for obvious reasons.
A subsidiary cannot have a subsidiary that is also a subsidiary of the company
So a subsidiary can also have subsidiaries, so its unlimitedly nested
what im also not sure about below is that a subsidiary is a company
class Company < ActiveRecord::Base
has_many :subsidiaries
end
class Subsidiary < ActiveRecord::Base
belongs_to :companies
end
Im sure this is so wrong, just putting something in here
UPDATE:
Ok, so I followed the instructions below like this:
class Company < ActiveRecord::Base
validates :name, presence: true
belongs_to :company
has_many :subsidiaries, foreign_key: 'company_id', class_name: 'Company'
end
In one of my templates:
<% @companies.each do |company| %>
<li><%= link_to "#{company.name} #{company.subsidiaries.length > 0 ? "(#{company.subsidiaries.length} subsidiaries)" :"" }", company_path(@company, :id => company.id) %></td>
<% end %>
Now this is show wrong, what happens is that the Ones with subsidiaries shows they have no subsidiaries and the ones who are subsidiaries shows they have subsidiaries, SO basicly its showing its parent's now, its "children"
ANy idea why this happens?