简体   繁体   English

RoR Activerecord:通过共享一个模型的两个关联来加入查询

[英]RoR Activerecord: joining query with two associations that share a model

I have a model called flightleg: 我有一个名为flightleg的模型:

class FlightLeg < ActiveRecord::Base
      ....
      belongs_to :departure_airport, :class_name => "Airport"
      belongs_to :arrival_airport, :class_name => "Airport"
end

And I want to do a query on it like this: 我想这样查询:

Flight.joins(:airline, flight_legs: [:departure_airport, :arrival_airport]).where('departure_airport.icao_code = YBBN')

Of course, this doesn't work. 当然,这是行不通的。 Here is a gist of the error message: 这是错误消息的要点:

https://gist.github.com/emilevictor/b1b7d18d5cede597c6be https://gist.github.com/emilevictor/b1b7d18d5cede597c6be

I am trying to figure out how to get it all to work nicely, and be able to refer to fields of the departure and arrival airports in my query. 我试图弄清楚如何使它们一切正常运行,并能够在我的查询中引用出发和到达机场的字段。

There's no table 'departure_airports', but in the gist we see 没有表'departure_airports',但是在要点上,我们看到了

 INNER JOIN "airports" ON "airports"."id" = "flight_legs"."departure_airport_id"

so can you try ('airports.icao_code = YBBN') ? 所以您可以尝试('airports.icao_code = YBBN')吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM