Ruby on rails ActiveRecord double所属的不包括外部关系

Ruby on rails ActiveRecord double所属的不包括外部关系,ruby-on-rails,activerecord,model,Ruby On Rails,Activerecord,Model,在Rails 3.2中,我有一个包含单词和引用的字典,名为gotowords,它在word_id中存储它们所属的单词,在reference_id中存储它们引用的单词,即模型中的gotofrom: create_table "words", :force => true do |t| t.string "word" t.text "definition" end create_table "gotowords", :force => true do |t| t

在Rails 3.2中,我有一个包含单词和引用的字典,名为gotowords,它在word_id中存储它们所属的单词,在reference_id中存储它们引用的单词,即模型中的gotofrom:

create_table "words", :force => true do |t|
  t.string   "word"
  t.text     "definition"
end

create_table "gotowords", :force => true do |t|
  t.integer "word_id"
  t.integer "reference_id"
end
class Word < ActiveRecord::Base
  has_many :gotowords
  has_many :gotofroms, class_name: "Gotoword", foreign_key: "reference_id"
end

class Gotoword < ActiveRecord::Base
  belongs_to :word
  belongs_to :gotofrom, class_name: "Word", foreign_key: "id"
end
对于模型:

create_table "words", :force => true do |t|
  t.string   "word"
  t.text     "definition"
end

create_table "gotowords", :force => true do |t|
  t.integer "word_id"
  t.integer "reference_id"
end
class Word < ActiveRecord::Base
  has_many :gotowords
  has_many :gotofroms, class_name: "Gotoword", foreign_key: "reference_id"
end

class Gotoword < ActiveRecord::Base
  belongs_to :word
  belongs_to :gotofrom, class_name: "Word", foreign_key: "id"
end
我现在不能像建议的那样重构,因为应用程序非常庞大,而且会产生太多的后果。也就是说,我可以接受补充查询,但它让我感到厌烦。。。按原样添加逆_无法解决问题:

has_many :gotowords, inverse_of: :word
has_many :gotofroms, class_name: "Gotoword", foreign_key: "reference_id", inverse_of: :gotofrom
是否有一种解决方案可以在该配置中包含Word两次?

尝试使用预加载。它的工作原理稍有不同,但可能仍有助于消除重复的db查询:

@words = Word.preload(:gotowords, :gotofroms)

谢谢,看起来很有希望,但我仍然有重复的查询。谢谢你,我发现了Earge_load,它的性能稍好一些,但也不能解决重复的查询。奇怪的是,这没有帮助。你能从你的日志中发布db查询吗?预加载或包含的db查询,后面是急切加载的db查询,以防万一:这真的很奇怪。也许它与视图中的某些代码更相关?