我正在运行这个 ActiveRecord 语句
@items = Item.joins(:order => :person)
.select('items.*').select('orders.*')
.includes(:order => [:person, :organization])
.order('created_at DESC')
.limit(10)
这些是查询:
SELECT items.*, orders.* FROM items INNER JOIN orders ON orders.id = items.order_id INNER JOIN people ON people.id = orders.person_id WHERE items.deleted_at IS NULL ORDER BY created_at DESC LIMIT 10
Item Load (0.001ms)
SELECT (trace)
SELECT orders.* FROM orders WHERE orders.deleted_at IS NULL AND orders.id IN (51, 50, 49, 48, 47, 46) ORDER BY orders.created_at DESC
Order Load (0.000ms)
SELECT (trace)
SELECT people.* FROM people WHERE people.deleted_at IS NULL AND people.id IN (11, 22, 21, 19, 18)
Person Load (0.000ms)
SELECT (trace)
SELECT organizations.* FROM organizations WHERE organizations.id IN (1)
Organization Load (0.000ms)
SELECT items.*, orders.*
如果 ActiveRecord 在第一个查询中已经使用 INNER JOIN 进行了选择,为什么还要从数据库中重新选择数据?如何在不返回数据库的情况下让它为 item.order 补水?