我有以下ActiveRecord模型:
class Person
belongs_to :favourite_car, class_name: 'Car'
belongs_to :business_car, class_name: 'Car'
belongs_to :home_car, class_name: 'Car'
end
当我想访问所有这三个关联时,它会生成三个选择查询:
SELECT * FROM cars WHERE cars.id = ?
这基本上是 N + 1问题。
理想情况下,我希望只生成一个表单
的查询SELECT * FROM cars WHERE cars.id IN (?, ?, ?)
我可以将此移动到与连接表中的列的has_many :through => :join_table
关联,以指示关联类型是什么,然后使用includes([:join_table, :cars])
来急切加载关联。但是,在这种情况下,它只将3个查询减少到2,并引入了一个额外的表。
另一种可能的解决方案是手动加载关联,如下所示:
module EagerLoader
def eager_load(*associations)
reflections = associations.map { |association| self.class.reflections[association.to_sym] }
raise 'Not all are valid associations' if reflections.any?(&:nil?)
reflections.group_by { |association| association.klass }.each do |klass, reflections|
load_associations(klass, reflections)
end
self
end
private
def load_associations(klass, reflections)
primary_key = klass.primary_key
ids = reflections.map { |reflection| public_send(reflection.foreign_key) }
records = klass.where(id: ids)
reflections.each_with_index do |reflection, i|
record = records.find do |record|
record.public_send(primary_key) == ids[i]
end
public_send("#{reflection.name}=", record)
end
end
end
我已经测试过了,它确实有效。
class Person
include EagerLoader
end
Person.find(2).eager_load(:favorite_car, :business_car, :home_car)
但是,当您想要执行
之类的操作时,这仍然无法帮助您Person.includes(:favourite_car, :business_car, :home_car)
例如,在人员索引页面上。这会将查询数量从3N + 1减少到4,但实际上只需要2个。
这个问题有更好的解决方案吗?
答案 0 :(得分:7)
试试这个:
> person_id = 1
> person = Person.includes(:favorite_car, :business_car, :home_car).where("people.id" = ?", person_id).references(:favorites_car, :business_car, :home_car)
> person[0].favorite_car
应用/模型/ person.rb 强>
class Person < ActiveRecord::Base
# columns: id, name, favorite_car_id, business_car_id, home_car_id
belongs_to :favorite_car, class_name: 'Car'
belongs_to :business_car, class_name: 'Car'
belongs_to :home_car, class_name: 'Car'
end
应用/模型/ car.rb 强>
class Car < ActiveRecord::Base
# columns: id, name
has_many :people
end
证明这是有效的:
> Person.all
Person Load (0.3ms) SELECT "people".* FROM "people"
=> #<ActiveRecord::Relation [#<Person id: 1, favorite_car_id: 1, business_car_id: 2, home_car_id: 3, name: "Frankie", created_at: "2014-02-18 21:51:58", updated_at: "2014-02-18 21:53:34">]>
> Car.all
Car Load (0.3ms) SELECT "cars".* FROM "cars"
=> #<ActiveRecord::Relation [#<Car id: 1, name: "Mazda", created_at: "2014-02-18 21:52:16", updated_at: "2014-02-18 21:52:16">, #<Car id: 2, name: "Honda", created_at: "2014-02-18 21:52:20", updated_at: "2014-02-18 21:52:20">, #<Car id: 3, name: "BMW", created_at: "2014-02-18 21:52:24", updated_at: "2014-02-18 21:52:24">]>
> Person.includes(:favorite_car, :business_car, :home_car).where("people.id = ?", 1).references(:favorite_car, :business_car, :home_car)
SQL (0.4ms) SELECT "people"."id" AS t0_r0, "people"."favorite_car_id" AS t0_r1, "people"."business_car_id" AS t0_r2, "people"."home_car_id" AS t0_r3, "people"."name" AS t0_r4, "people"."created_at" AS t0_r5, "people"."updated_at" AS t0_r6, "cars"."id" AS t1_r0, "cars"."name" AS t1_r1, "cars"."created_at" AS t1_r2, "cars"."updated_at" AS t1_r3, "business_cars_people"."id" AS t2_r0, "business_cars_people"."name" AS t2_r1, "business_cars_people"."created_at" AS t2_r2, "business_cars_people"."updated_at" AS t2_r3, "home_cars_people"."id" AS t3_r0, "home_cars_people"."name" AS t3_r1, "home_cars_people"."created_at" AS t3_r2, "home_cars_people"."updated_at" AS t3_r3 FROM "people" LEFT OUTER JOIN "cars" ON "cars"."id" = "people"."favorite_car_id" LEFT OUTER JOIN "cars" "business_cars_people" ON "business_cars_people"."id" = "people"."business_car_id" LEFT OUTER JOIN "cars" "home_cars_people" ON "home_cars_people"."id" = "people"."home_car_id" WHERE (people.id = 1)
=> #<ActiveRecord::Relation [#<Person id: 1, favorite_car_id: 1, business_car_id: 2, home_car_id: 3, name: "Frankie", created_at: "2014-02-18 21:51:58", updated_at: "2014-02-18 21:53:34">]>
重要说明: Rails会自动使用people
作为person
的复数形式。因此,当您创建Person
模型时,它将创建一个people
数据库表。
答案 1 :(得分:3)
关于手动预先加载的帖子很棒。
我想这就是你一直在寻找的东西:
owners = People.all
association_name = :photos
owners.each do |owner|
record = whatever_you_want
association = owner.association(association_name)
association.target = record
association.set_inverse_instance(record)
end
答案 2 :(得分:0)
我个人创建了一个连接表。话虽如此,这是另一种选择:可以基于SQL查询定义关系:
class Person
belongs_to :favourite_car, class_name: 'Car'
belongs_to :business_car, class_name: 'Car'
belongs_to :home_car, class_name: 'Car'
has_many :cars, class_name: 'Car', :finder_sql => %q(
SELECT DISTINCT cars.*
FROM cars
WHERE cars.id IN (#{c_ids})
)
def c_ids
[favourite_car_id, business_car_id, home_car_id].compact.uniq.join(',')
end
end
然后你可以做Person.includes(:cars)