我有这个有效的查询:
@search = Availability.joins{facility.activities}
.where{activities.id == s_activity}
但我的观点是从设施获得了大量信息,这导致了N + 1问题。
所以我决定使用includes
来代替加载我的关联
@search = Availability.includes{facility.activities}
.where{ facility.activities.id == s_activity)}
但这会导致错误:
!! #<ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "activities"
LINE 1: ...T "availabilities".* FROM "availabilities" WHERE ("activitie...
^
这些是协会:
class Activity < ActiveRecord::Base
has_and_belongs_to_many :facilities
end
class Availability < ActiveRecord::Base
# Associations
belongs_to :facility
end
class Facility < ActiveRecord::Base
# Associations
has_and_belongs_to_many :activities
has_many :availabilities
end
activities_facilities
has_and_belongs_to_many
的表格
答案 0 :(得分:1)