この 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.*
最初のクエリで INNER JOIN を使用して既に SELECT されている場合、ActiveRecord がデータベースからデータを再選択するのはなぜですか? DBに戻らずにitem.orderを水和させるにはどうすればよいですか?