简体   繁体   English

Rails模型has_many

[英]Rails models has_many

Im starting to learn rails today and not quite sure how this would work: 我今天开始学习rails并不太确定它是如何工作的:

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" 现在这是错误的,所发生的事情是,有子公司的Ones显示他们没有子公司,而子公司显示他们有子公司,所以基本上它现在显示它的父母,它的“孩子”

ANy idea why this happens? 不知道为什么会这样?

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. 然后,您将使用验证来检查所有这些条件。

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: 要使用这些关系,您需要在表中使用parent_id列:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM