<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>HWPS! &#187; acts-as-taggable-on-steroids</title>
	<atom:link href="http://holy.enyou.org/tag/acts-as-taggable-on-steroids/feed/" rel="self" type="application/rss+xml" />
	<link>http://holy.enyou.org</link>
	<description>HoLY's Web Press Site</description>
	<lastBuildDate>Fri, 28 Jan 2011 04:19:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>acts_as_taggable_on_steroids と will_paginate を同時に使う</title>
		<link>http://holy.enyou.org/2008/11/26/acts_as_taggable_on_steroids_with_will_paginate/</link>
		<comments>http://holy.enyou.org/2008/11/26/acts_as_taggable_on_steroids_with_will_paginate/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 16:44:28 +0000</pubDate>
		<dc:creator>HoLY</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[acts-as-taggable-on-steroids]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[will-paginate]]></category>

		<guid isPermaLink="false">http://holy.enyou.org/2008/11/26/acts_as_taggable_on_steroids_with_will_paginate/</guid>
		<description><![CDATA[Rails2 で acts_as_taggable_on_steroids でタグ付けしたモデル（ここでは Entry とする）のリストを will_paginate で paginate する方法。

以下のようにすると、全該当 Entry を find(:all) してしまうので、精神衛生上いまいち。(ということにしておく)

Entry.find_tagged_with('rails').paginate(:page =&#62; params[:page], :per_page =&#62; 30)


find(:all) することなしに、こんな風に書けるようにするには。

@pages = Entry.paginate_tag 'rails',
    :page =&#62; params[:page],
    :per_page =&#62; 30


lib/paginate_tag.rb をこんな内容で作り、environment.rb で読み込む。

module ActiveRecord
  module Acts #:nodoc:
    module Taggable #:nodoc:
      module SingletonMethods

 [...]]]></description>
			<content:encoded><![CDATA[<p>Rails2 で acts_as_taggable_on_steroids でタグ付けしたモデル（ここでは Entry とする）のリストを will_paginate で paginate する方法。</p>

<p>以下のようにすると、全該当 Entry を find(:all) してしまうので、精神衛生上いまいち。(ということにしておく)</p>

<pre><code>Entry.find_tagged_with('rails').paginate(:page =&gt; params[:page], :per_page =&gt; 30)
</code></pre>

<p>find(:all) することなしに、こんな風に書けるようにするには。</p>

<pre><code>@pages = Entry.paginate_tag 'rails',
    :page =&gt; params[:page],
    :per_page =&gt; 30
</code></pre>

<p>lib/paginate_tag.rb をこんな内容で作り、environment.rb で読み込む。</p>

<pre><code>module ActiveRecord
  module Acts #:nodoc:
    module Taggable #:nodoc:
      module SingletonMethods

        def count_tagged_with(name, *args)
          options = find_options_for_find_tagged_with(name, *args)
          options.delete :select
          # この辺はもうちょいスマートにいけないかな?
          options.delete :page
          options.delete :per_page
          options.delete :total_entries
          count(options)
        end

        def paginate_tag(name, *args)
          count = count_tagged_with(name, *args)
          options = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}

          paginate_tagged_with(name, {
            :per_page =&gt; 30,
            :total_entries =&gt; count,
          }.merge(options))
        end

      end
    end
  end
end
</code></pre>

<p>ただしこのやり方では find(:all) はしないかわりに COUNT (*) クエリを別途呼んでしまうので諸刃の剣。どっちが速いかは場合によるだろう。</p>

<p>ていうか cache_fu してしまえば一緒なのかも。（要追試。）</p>

<p>will_paginate は paginate_xxx メソッドを呼ぶと内部で find_all_xxx を使ってくれるのだが、素直に paginate_tagged_with するだけだと、count の正しい値が取れない。というか count の値を正しく取れることはあるんだろーか…。</p>
]]></content:encoded>
			<wfw:commentRss>http://holy.enyou.org/2008/11/26/acts_as_taggable_on_steroids_with_will_paginate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

