Ale*_*ore 3 ruby-on-rails will-paginate ruby-on-rails-3.1
我的应用程序因N + 1查询而变慢.我正在使用带有Rails 3.1和Oracle 11.2的will_paginate gem.
通常情况下,解决方案是使用ActiveRecord#includes()在我获取父记录的同时读入子记录,但是我所做的每一次尝试似乎都不能使用will_paginate,或者最终获取整个数据库进入内存.我尝试制作一个命名范围,但似乎在调用时立即获取范围,而不是懒惰.
class Parent < ActiveRecord::Base
has_many :children, :dependent => :delete_all
end
class Child < ActiveRecord::Base
belongs_to :parent
end
class ParentsController < ApplicationController
def index
@parents = Parent.paginate page: params[:page], order: 'id', per_page: 32
end
Run Code Online (Sandbox Code Playgroud)
Index.html.erb包含,除其他外,
<%= page_entries_info @parents %>
<%= will_paginate @parents, :container => false %>
<% @parents.each do |parent| %>
<%= parent.call_to_method_that_uses_children_association_causing_N+1_query %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
如果我无法将整个数据库提取到内存中,并且不想获取父页面,然后是N个子提取,除了放弃will_paginate之外还有其他选择吗?
另外,will_paginate还有大量文档吗?github上的信息很稀疏.例如,will_paginate方法的:container选项是什么意思?
Parent.includes(:children).paginate(:page => params[:page], :per_page => 30)
Run Code Online (Sandbox Code Playgroud)