如何在 Rails ActiveRecord 中一次执行多个 JOINS

How to do multiple JOINS at once in Rails ActiveRecord

我得到了这两个 table,其中一个 table 有多个指向第二个 table 的外键。

Table rankings

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| search_text    | varchar(255) | YES  | MUL | NULL    |                |
| first_item_id  | bigint(20)   | YES  | MUL | NULL    |                |
| second_item_id | bigint(20)   | YES  | MUL | NULL    |                |
| third_item_id  | bigint(20)   | YES  | MUL | NULL    |                |
| forth_item_id  | bigint(20)   | YES  | MUL | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+

Table items

+---------------------------+--------------+------+-----+---------+----------------+
| Field                     | Type         | Null | Key | Default | Extra          |
+---------------------------+--------------+------+-----+---------+----------------+
| id                        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| item_code                 | varchar(255) | YES  | MUL | NULL    |                |
+----------------+--------------+------+-----+---------+---------------------------+

样本数据rankings

+----+-------------+-------+--------+-------+-------+
| id | search_text | first | second | third | forth |
+----+-------------+-------+--------+-------+-------+
|  1 | test 1      |     1 |      2 |     3 |     4 |
|  2 | test 2      |     1 |      2 |     3 |     4 |
|  3 | test 3      |     1 |      2 |     3 |     4 |
|  4 | test 4      |     1 |      2 |     3 |     4 |
+----+-------------+-------+--------+-------+-------+

样本数据items

+--------+------------+
| id     | item_code  |
+--------+------------+
|      1 | 125659     |
|      2 | 125660     |
|      3 | 125661     |
|      4 | 125662     |
+--------+------------+

目前我正在检索排名数据如下:

@rankings = Admin::Ranking.all

预期数据

+----+-------------+-------+--------+-------+-------+
| id | search_text | first | second | third | forth |
+----+-------------+-------+--------+-------+-------+
|  1 | test 1      | 125659| 125660 | 125661| 125662|
|  2 | test 2      | 125659| 125660 | 125661| 125662|
|  3 | test 3      | 125659| 125660 | 125661| 125662|
|  4 | test 4      | 125659| 125660 | 125661| 125662|
+----+-------------+-------+--------+-------+-------+

检索预期数据的SQL查询:

SELECT 
  r.id,
  r.search_text,
  i1.item_code AS first,
  i2.item_code AS second,
  i3.item_code AS third,
  i4.item_code AS forth
FROM rankings r
LEFT JOIN items i1 ON i1.id = r.first_item_id
LEFT JOIN items i2 ON i2.id = r.second_item_id
LEFT JOIN items i3 ON i3.id = r.third_item_id
LEFT JOIN items i4 ON i4.id = r.forth_item_id
ORDER BY r.id;

型号类如下:

items -> Item, rankings -> Ranking

关于如何使用 Rails ActiveRecord 以预期格式检索排名数据的任何建议?

为每个外键创建一个单独的关联。

class Ranking < ApplicationRecord
  belongs_to :first_item, class_name: 'Item'
  belongs_to :second_item, class_name: 'Item'
  belongs_to :third_item, class_name: 'Item'
  belongs_to :forth_item, class_name: 'Item'
end

然后你可以只包含所有这些并根据需要进行查询。

rankings = Ranking.all.includes(:first_item, :second_item, :third_item, :forth_item)
rankings.each do |ranking|
  ranking.first_item.item_code # example
end

这是ActiveRecord方式。如果你只想得到 item_code-s 而没有别的,你可以 .select 它代替。