Rails models has_many

有些话、适合烂在心里 提交于 2020-01-07 06:36:28

问题


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?


回答1:


What you want is a recursive self relation:

class Company < ActiveRecord::Base
    belongs_to :company
    has_many :subsidiaries, foreign_key: 'company_id', class_name: 'Company'
end

So, essentially, a company belongs to a company and has many companies. However, we have a special name for it's companies (subsidiaries), so we give it this "alias" by manually setting the relation. You might want to do the same for the "parent" company.

You would then use validations to check all those conditions.




回答2:


I would use

class Company < ActiveRecord::Base
  has_many   :subsidiaries, class_name: 'Company', foreign_key: :parent_id
  belongs_to :parent, class_name: 'Company'
end

To use these relations you will need parent_id column in your table:

rails g migration add_parent_id_to_companies parent_id:ineteger
rake db:migrate

You would use like this:

          A
        /   \
       /     \
      B       C
     / \
    /   \
   D     E


A.subsidiaries => [B,C]
B.subsidiaries => [D,E]
C.subsidiaries => [] # actually a relation without results
B.parent => A
C.parent => A
A.parent => nil
D.parent => B


来源:https://stackoverflow.com/questions/17726359/rails-models-has-many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!