【问题标题】:Joins in rails between three tables三个表之间的导轨连接
【发布时间】:2018-11-22 01:32:51
【问题描述】:

我有三张表,父母、孩子和资金。

父.rb

class Parent < ApplicationRecord
  has_many :children, dependent: :destroy
end

child.rb

class Parent < ApplicationRecord
  belongs_to :parent
  has_many :fundings, dependent: :destroy
end

funding.rb

class Funding < ApplicationRecord
      belongs_to :child
end

儿童和资金之间的联系

create_table "children_fundings", id: false, force: :cascade do |t|
    t.integer "child_id", null: false
    t.integer "funding_id", null: false
    t.index ["child_id", "funding_id"], name: 
    "index_children_fundings_on_child_id_and_funding_id"
    t.index ["funding_id", "child_id"], name: 
    "index_children_fundings_on_funding_id_and_child_id"
end

加入孩子和父母之间

  create_table "children_parents", id: false, force: :cascade do |t|
    t.integer "parent_id", null: false
    t.integer "child_id", null: false
    t.index ["child_id", "parent_id"], name: 
    "index_children_parents_on_child_id_and_parent_id"
    t.index ["parent_id", "child_id"], name: 
    "index_children_parents_on_parent_id_and_child_id"
  end

children 表有 parent_id,fundings 表有 child_id。如何在父母子女和资金表之间创建联接。请帮忙

【问题讨论】:

    标签: ruby-on-rails ruby join ruby-on-rails-5 jquery-datatables-rails


    【解决方案1】:

    您不需要在这里连接表 - 而是子记录上的父 ID 列。所以,在你的情况下:

    • Child 需要一个整数列 parent_id
    • Funding 需要一个整数列 child_id

    只有在您实现 has_and_belongs_to_manyhas_many through 关系时,联接表才会发挥作用。

    如果您考虑如何将记录联系在一起,让孩子属于父母,孩子只需要知道它与哪个父母相关联,因此列。

    现在,假设父母有很多孩子,孩子有很多父母:单个父母 ID 无法解决问题,因此加入表将两者联系在一起,包含(例如)parent_id 和 @987654328 @ 在每一行数据中。

    数据库使用这些方法将记录联系在一起,根据需要查询 id 或连接表。

    要从父记录中访问资金,如果它们独立相关,您可以将两者链接在一起,如果不相关,则通过子记录访问。

    对于后者,您可以使用类似以下的内容。

    在您的控制器中:

    @parents = Parent.includes(children: :fundings) # includes preloads the data, avoiding expensive N + 1 queries
    

    在你看来:

    <% @parents.each do |parent| %>
      <#% whatever you want to display regarding the parent here %>
    
      <% parent.children.each do |child| %>
        <#% whatever you want to display regarding the child here %>
    
        <% child.fundings.each do |funding| %>
          <#% whatever you want to display regarding the funding here %>
        <% end %>
      <% end %>
    <% end %>
    

    这有点混乱,因此值得将控制器中的数据或部分数据分开。希望这能让您了解如何工作?

    【讨论】:

    • child 已经有一个 parent_id,funding 已经有一个 child_id。我需要这个连接表来查看父视图页面中的资金表
    • 您需要查看什么fundings?父母子女的资助?
    • 感谢 Navroop - 在这种情况下,这个更新的答案应该可以工作(@widjajayd 也提出了一个很好的观点,即使用 as has many through 快捷方式)。如果您有任何其他问题,请告诉我您的进展情况!
    【解决方案2】:

    如果您需要从父母那里获得资金,您可以使用 has_many 通过在 rails 中声明如下:

    父.rb

    class Parent < ApplicationRecord
      has_many :children, dependent: :destroy
      has_many :fundings, through: :children
    end
    

    child.rb

    class Child < ApplicationRecord
      belongs_to :parent
      has_many :fundings, dependent: :destroy
    end
    

    funding.rb

    class Funding < ApplicationRecord
      belongs_to :child
    end
    

    您可以通过 @parent.fundings

    通过父母访问资金记录

    这是has_many through的参考链接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      相关资源
      最近更新 更多