I want to do a query, but query speed is slow,I have five models like this:
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
than, I insert some test data to mysql:(seed)
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)
}
}
}
in my controller, I do like this:
def index
@ms = Mya.first.to_json(:include => [{
mybs: {
:include => {
:mycs => {
:include => [:myds, :myes]
}
}
}
}
])
render json: @ms
end
It is very very slow, help me, thanks. sorry about my english.