ActiveRecord(Rails)中的多级嵌套计数

时间:2013-11-24 04:25:06

标签: ruby-on-rails ruby activerecord

我觉得这里有点深度。我有以下关系:

class User < ActiveRecord::Base
    has_many :flights
end

class Flight < ActiveRecord::Base
    has_many :flight_legs
    belongs_to :user
end

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

class Airport < ActiveRecord::Base
      belongs_to :country
      has_many :flight_legs_arriving_here, :class_name => "FlightLeg",
        :foreign_key => "arrival_airport_id"
      has_many :flight_legs_departing_here, :class_name => "FlightLeg",
        :foreign_key => "departure_airport_id"
end

class Country < ActiveRecord::Base
    attr_accessible :name
    has_many :airports
end

我希望按用户访问过的国家/地区数量获取用户列表,然后单独查询将返回用户访问过的国家/地区数量。有谁知道怎么做?我对此文档的详细信息感到遗憾。

0 个答案:

没有答案