模型hirerachy struture是城市HAS_MANY机场,机场HAS_MANY航班
如何在这种雇佣结构中避免N + 1查询。
我可以通过
修复机场的N + 1查询@flights = Flight.includes(:to_airport, :from_airport, :airline).all
但仍有N + 1查询用于访问城市
json.array!(@flights) do |flight|
json.from_airport flight.from
json.to_airport flight.to
json.from_city flight.from_airport.city.name
json.to_city flight.to_airport.city.name
end
答案 0 :(得分:0)
Flight.includes(to_airport: :city, :airline)