开发者

What's special about :conditions => [a_field IN (.., .., ..)]?

开发者 https://www.devze.com 2023-01-21 19:03 出处:网络
The following result sets work well with will_paginate: Members.all(:limit => 5).paginate(:page => params[:page])

The following result sets work well with will_paginate:

Members.all(:limit => 5).paginate(:page => params[:page])
Members.all(:conditions => ["member_no < 6"]).paginate(:page => params[:page])
Members.all.paginate(:page => params[:page])

The following does not:

Members.all(:conditions => ["member_no IN (?)", [1, 2, 3, 4, 5]]).paginate(:page => params[:page])

Why the second query does not work we开发者_如何学编程ll with paginate? Thanks!


The #paginate is an instance method made available on Array and ActiveRecord::Base. You really should be doing it this way:

Member.paginate(:page => params[:page], :limit => 5)
Member.paginate(:conditions => ["member_no < ?", 6], :page => params[:page])
Member.paginate(:page => params[:page])
Member.paginate(:conditions => {:member_no => (1..5)}, :page => params[:page])

When you call #all then #paginate, what you're doing is asking for all members (all 1,000,000 of them), then discarding 99.999% of them, because you only want the first 10. This is very wasteful, to say the least.

0

精彩评论

暂无评论...
验证码 换一张
取 消