我想做一个查询,但查询速度很慢,我有五个这样的模型:
class Mya < ActiveRecord::Base
has_many :mybs
end
class Myd < ActiveRecord::Base
belongs_to :myc
end
class Myc < ActiveRecord::Base
belongs_to :myb
has_many :myds
has_many :myes
end
class Myd < ActiveRecord::Base
belongs_to :myc
end
class Mye < ActiveRecord::Base
belongs_to :myc
end
Run Code Online (Sandbox Code Playgroud)
比,我把一些测试数据插入mysql :(种子)
mya=Mya.create!(title: 'first test')
i=0
10.times{
i=i+1
myb=Myb.create!(title: "my_#{i}")
5000.times{
myc=Myc.create!(mya_id: mya.id, myb_id: myb.id)
4.times {
myd=Myd.create!(mya_id: mya.id, myb_id: myb.id, myc_id: myc.id)
mye=Mye.create!(mya_id: mya.id, myb_id: myb.id, myc_id: myc.id)
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我喜欢这样:
def index
@ms = Mya.first.to_json(:include => [{ …Run Code Online (Sandbox Code Playgroud)