我有相当复杂的关联,我使用awesone_nested_set gem来管理Ratecard::RuleIndex
父母和孩子:
class Inventory::Inventory < ApplicationRecord
has_many :ratecards_rules, class_name: 'Ratecard::RatecardsRule', through: :ratecards
has_many :rule_indices, class_name: 'Ratecard::RuleIndex', through: :ratecards do
def children
Ratecard::RuleIndex.each_with_level(Ratecard::RuleIndex.root.children) do |index, level|
puts index.index_value
end
end
end
end
class Ratecard::Ratecard < ApplicationRecord
has_many :ratecards_rules, class_name: 'Ratecard::RatecardsRule'
has_many :rule_indices, class_name: 'Ratecard::RuleIndex',
through: :ratecards_rules, source: :rulable,
source_type: 'Ratecard::RuleIndex'
end
class Ratecard::RatecardsRule < ApplicationRecord
belongs_to :rulable, polymorphic: true, optional: true
end
class Ratecard::Rule < ApplicationRecord
has_many :ratecards_rules, class_name: 'Ratecard::RatecardsRule', as: :rulable
has_many :ratecards, class_name: 'Ratecard::Ratecard', through: :ratecards_rules
end
class Ratecard::RuleIndex < Ratecard::Rule
has_many :ratecards_rules, class_name: 'Ratecard::RatecardsRule', as: :rulable
has_many :ratecards, class_name: 'Ratecard::Ratecard', through: :ratecards_rules
end
在我的控制台中,我可以这样做:
i=Inventory::Inventory.where(id: [200,201,202]).includes(:ratecards)
.where(ratecard_ratecards: {id: 1}).preload(:rule_indices)
预加载Ratecard::RuleIndex
父记录(索引)。但是我需要以某种方式预加载儿童记录。在我做的那一刻
i.collect{|x| {indices: x.rule_indices.children.collect{|i| {id: i.index_value}} }}
我得到了我想要的输出,但是执行了额外的查询,它看起来像他的:
Ratecard::RuleIndex Load (0.8ms) SELECT "ratecard_rules".* FROM "ratecard_rules" WHERE "ratecard_rules"."type" IN ('Ratecard::RuleIndex') AND "ratecard_rules"."parent_id" IS NULL ORDER BY "ratecard_rules"."lft" ASC LIMIT $1 [["LIMIT", 1]]
Ratecard::Rule Load (0.5ms) SELECT "ratecard_rules".* FROM "ratecard_rules" WHERE "ratecard_rules"."parent_id" = $1 ORDER BY "ratecard_rules"."lft" ASC [["parent_id", 60]]
0.95
=> [{:indices=>[{:id=>0.95e0}]}]
是否有某种方法可以预加载(由于多态关联并且特别需要“预加载”)我的子记录,这样我就不会收到以后的 N+1 个查询? 我会很高兴任何提示。谢谢你。
到目前为止,我一直在尝试实现has_many associaion extension,但它并没有给我想要的结果,因为我无法让我的孩子记录被预加载。我知道这是因为我的查询转到了 Ratecard::RatecardsRule,其中我的“父”索引记录由rulable_type
and抓取rulable_id
。如果我能以某种方式预加载子记录(与父母一起存储在“ratecard_rules”表中并使用 Awesome Nested Set gem 管理),那就太好了。