/ / 最新 / 2009-09 / RSS / twitter / tumblr / 09014502501 / mail@ssig33.com

屋久島沈没


oreore_paginate_by_sql

OreOre Twitter Search とかで使ってるやつ、 will_paginate/lib/will_paginate/finder.rb とかに書く

def oreore_paginate_by_sql(sql, count_query, options)
	WillPaginate::Collection.create(*wp_parse_options(options)) do |pager|
		query = sanitize_sql(sql)
		original_query = query.dup
		# add limit, offset
		add_limit! query, :offset => pager.offset, :limit => pager.per_page
		# perfom the find
		pager.replace find_by_sql(query)

		unless pager.total_entries
			pager.total_entries = count_by_sql(count_query)
		end
	end
end

:
標準の paginate_by_sql は、投げられたクエリを一度実行して件数数えてから云々、みたいな動作をするのだけど、それだと数えるのに時間がかかって使い物にならないことがあるので、カウント用のクエリも自前で投げられるようにしたもの。まあ数行いじっただけですね。

shit = Shit.oreore_paginate_bysql(本来のクエリ, カウント用クエリ, options)

みたいに使う。本来のクエリで数えられると遅い場合に速く数えられるクエリを設定して使う感じ。

カウントが若干ずれてもいいけど速くしたいときに使う。


will_paginate はオリジナルであるものをなんとなく眺めていれば、おれおれページネーションを簡単に作れるのがいいと思う。

blog comments powered by Disqus

Referrer (Inside):

[ 固定リンク ]