<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/atom10full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xml:lang="en-US">
  <title>dead dead good - Home</title>
  <id>tag:deaddeadgood.com,2008:mephisto/</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  
  <link href="http://deaddeadgood.com/" rel="alternate" type="text/html" />
  <updated>2008-08-27T11:19:44Z</updated>
  <link rel="self" href="http://feeds.feedburner.com/deaddeadgood" type="application/atom+xml" /><entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-08-27:37</id>
    <published>2008-08-27T11:17:00Z</published>
    <updated>2008-08-27T11:19:44Z</updated>
    <category term="activemodel" />
    <category term="activerecord" />
    <category term="activeresource" />
    <category term="callbacks" />
    <category term="rails" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/376107838/active-resource-callbacks" rel="alternate" type="text/html" />
    <title>Active Resource Callbacks</title>
<content type="html">
            &lt;p&gt;It looks like &lt;a href="http://github.com/rails/rails/tree/master/activemodel"&gt;work is still on-going&lt;/a&gt; to &lt;a href="http://weblog.techno-weenie.net/2008/1/21/random-rails-tidbits"&gt;extract functionality common to Active Record and Active Resource into a new Active Model library&lt;/a&gt;. Excellent. But I wanted to add a couple of callbacks to Active Resource and I wanted to do it quickly. Thankfully, &lt;a href="http://joshpeek.com/"&gt;someone much smarter than me&lt;/a&gt; has already &lt;a href="http://github.com/rails/rails/commit/aae37bb4f7edd6a1820e420a60560369c6064f33"&gt;extracted callbacks from Active Resource&lt;/a&gt;, so getting something working was pretty straight forward.&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;module ActiveResource
  module Callbacks
    CALLBACKS = %w( after_save after_destroy )
    
    def self.included(base)
      [:save, :destroy].each do |method|
        base.send :alias_method_chain, method, :callbacks
      end
      
      base.send :include, ActiveSupport::Callbacks
      base.define_callbacks *CALLBACKS
    end
    
    def save_with_callbacks
      returning save_without_callbacks do
        run_callbacks(:after_save) unless new?
      end
    end
    
    def destroy_with_callbacks
      returning destroy_without_callbacks do
        run_callbacks(:after_destroy)
      end
    end
  end
end

ActiveResource::Base.send :include, ActiveResource::Callbacks&lt;/code&gt;&lt;/pre&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/8/27/active-resource-callbacks</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-08-18:33</id>
    <published>2008-08-18T21:33:00Z</published>
    <updated>2008-08-18T21:34:45Z</updated>
    <category term="arduino" />
    <category term="barcamp" />
    <category term="ruby" />
    <category term="shoes" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/368450402/shoes-and-arduino-etch-a-sketch" rel="alternate" type="text/html" />
    <title>Shoes and Arduino Etch a Sketch</title>
<content type="html">
            &lt;p&gt;Here's a short video of me struggling to demo &lt;a href="http://deaddeadgood.com/assets/2008/8/18/etch_a_sketch.tar.gz"&gt;an Etch a Sketch app&lt;/a&gt; which formed part of a presentation I gave at &lt;a href="http://barcampleeds.com/"&gt;BarCamp Leeds&lt;/a&gt; as part of &lt;a href="http://www.simonwheatley.co.uk/2008/08/13/barcamp-leeds-my-last-project/"&gt;Simon Wheatley's 'My Last Project' session&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&amp;lt;object height="350" width="425"&gt;
&amp;lt;param&gt;&amp;lt;/param&gt;
&amp;lt;embed src="http://www.youtube.com/v/017loHbDUCE" height="350" width="425"&gt;
&amp;lt;/embed&gt;
&amp;lt;/object&gt;&lt;/p&gt;

&lt;p&gt;The code is a bit rough in places but might be of interest if like me you're getting started with either &lt;a href="http://arduino.cc/"&gt;Arduino&lt;/a&gt; or &lt;a href="http://shoooes.net/"&gt;Shoes&lt;/a&gt; development.&lt;/p&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/8/18/shoes-and-arduino-etch-a-sketch</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-08-03:31</id>
    <published>2008-08-03T10:17:00Z</published>
    <updated>2008-08-03T10:17:58Z</updated>
    <category term="activeresource" />
    <category term="gem" />
    <category term="pagination" />
    <category term="rails" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/354287427/resourceful_pagination" rel="alternate" type="text/html" />
    <title>resourceful_pagination</title>
<content type="html">
            &lt;p&gt;I've been doing quite a bit of work recently on an Active Resource project and needed to add pagination to one of our resources. I had a quick look around to see what other people were doing, and while it looks like &lt;a href="http://github.com/mislav/will_paginate/tree/agnostic/"&gt;work is underway to add support for Active Resource&lt;/a&gt; (and ORMs other than Active Record) to &lt;a href="http://github.com/mislav/will_paginate/"&gt;will_paginate&lt;/a&gt; I don't think it's quite ready just yet. So while we wait, may I present &lt;a href="http://github.com/phorsfall/resourceful_pagination/"&gt;resourceful_pagination&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to dive straight in, take a look at the &lt;a href="http://github.com/phorsfall/resourceful_pagination/tree/master/README.txt"&gt;readme&lt;/a&gt; then install the gem with:&lt;p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem install phorsfall-resourceful_pagination --source http://gems.github.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With the gem installed you get a shiny new &lt;code&gt;ActiveResource::Base#paginate&lt;/code&gt; method. Use it like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;MyResource.paginate(:page =&gt; params[:page])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This returns a &lt;code&gt;WillPaginate::Collection&lt;/code&gt; which you can use with will_paginate's view helpers as usual.&lt;/p&gt;

&lt;p&gt;Easy, right?, Well, sort of. The gem makes a few assumptions about your Active Resource class and your RESTful resource. If you want to use this you'll probably need to make a few changes. First you need to add two class methods to your Active Resources.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;code&gt;count&lt;/code&gt; method which returns the total number of items. This could call a custom count action on the app server.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;per_page&lt;/code&gt; method indicating how many records you require per page.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Something like this would do the trick:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;class MyResource &amp;lt; ActiveResource::Base
  @@per_page = 10
  cattr_reader :per_page

  def self.count
    get(:count)['count']
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You also need the &lt;code&gt;index&lt;/code&gt; action of the controller on the app server to handle the &lt;code&gt;:starting_at&lt;/code&gt; and &lt;code&gt;:per_page&lt;/code&gt; parameters. These map to &lt;code&gt;:offset&lt;/code&gt; and &lt;code&gt;:limit&lt;/code&gt; in SQL and can be passed straight through to Active Record.&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;MyResourcesController &amp;lt; ApplicationController
  def index
    MyResource.find :all, :limit =&gt; params[:per_page], :offset =&gt; params[:starting_at]
    # Render XML etc.
  end

  def count
    count = MyResource.count
    render :xml =&gt; { :count =&gt; count }
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And that's about it. There's a little bit more to do if you have a nested resource, but there's a bit more on that in the readme.&lt;/p&gt;

&lt;p&gt;Enjoy.&lt;/p&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/8/3/resourceful_pagination</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-06-29:29</id>
    <published>2008-06-29T18:50:00Z</published>
    <updated>2008-06-29T18:51:53Z</updated>
    <category term="rails" />
    <category term="rspec" />
    <category term="ruby" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/322733120/an-include_any-matcher-for-rspec" rel="alternate" type="text/html" />
    <title>An include_any Matcher for RSpec</title>
<content type="html">
            &lt;p&gt;Writing custom &lt;a href="http://rspec.rubyforge.org/rdoc/classes/Spec/Matchers.html"&gt;RSpec expectation matchers&lt;/a&gt; is a great way to enhance the readability of your specifications. As an example, here's a simple matcher to check whether a collection contains any of the given items. An example will make things clearer:&lt;/p&gt;

&lt;pre&gt;
&lt;code class="ruby"&gt;# Passing example
odds  = [1,3,5,7,9]
evens = [2,4,6,8]
odds.should_not include_any(*evens)&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;One thing to note is that &lt;code&gt;include_any&lt;/code&gt; expects multiple arguments rather than a collection, hence the splat. Here's the code:&lt;/p&gt;

&lt;pre&gt;
&lt;code class="ruby"&gt;module Spec
  module Matchers
    def include_any(*args)
      IncludeAny.new(*args)
    end
  
    class IncludeAny
      def initialize(*args)
        @args = args
      end
    
      def matches?(target)
        @target = target
        @target.include_any?(*@args)
      end
    
      def failure_message
        "expected #{@target.inspect} to include one or more elements from #{@args.inspect}"
      end
    
      def negative_failure_message
        "expected #{@target.inspect} not to include any elements from #{@args.inspect}"
      end
    end
  end
end&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;This is about as trivial as a custom matcher gets. All of the work is done by the &lt;code&gt;include_any?&lt;/code&gt; method, which doesn't actually exist as part of the &lt;a href="http://www.ruby-doc.org/core/"&gt;core Ruby API&lt;/a&gt;. In fact it's a simple extension to &lt;code&gt;Enumerable&lt;/code&gt; that I think I originally borrowed from &lt;a href="http://merbivore.com/"&gt;Merb&lt;/a&gt;. Put the following in &lt;code&gt;config/initializers/core_extensions.rb&lt;/code&gt; or similar to use it in a &lt;a href="http://www.rubyonrails.org/"&gt;Rails&lt;/a&gt; app.&lt;/p&gt;

&lt;pre&gt;
&lt;code class="ruby"&gt;module Enumerable
  def include_any?(*args)
    args.any? { |arg| self.include?(arg) }
  end
end&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;Which custom matchers are you using?&lt;/p&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/6/29/an-include_any-matcher-for-rspec</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-06-25:28</id>
    <published>2008-06-25T23:19:00Z</published>
    <updated>2008-06-25T23:22:28Z</updated>
    <category term="activerecord" />
    <category term="rails" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/320087857/how-to-bypass-attr_accessible-and-attr_protected-in-rails" rel="alternate" type="text/html" />
    <title>How to Bypass attr_accessible and attr_protected in Rails</title>
<content type="html">
            &lt;p&gt;Mass assignment in Rails is super handy and saves heaps of code, especially in your controllers. As everyone knows however, &lt;a href="http://railscasts.com/episodes/26"&gt;hackers love mass assignment&lt;/a&gt; so you really need to use &lt;code&gt;attr_accessible&lt;/code&gt; or &lt;code&gt;attr_protected&lt;/code&gt; to protect your models.&lt;/p&gt;

&lt;p&gt;But what happens if you want to mass assign model attributes which are otherwise protected? It would be simple enough to create your own method to do this, but if you take a look at &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001374"&gt;the source&lt;/a&gt; you'll see that Rails already has it covered. Here's the method signature for &lt;code&gt;ActiveRecord::Base#attributes=&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;def attributes=(new_attributes, guard_protected_attributes = true)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That optional second parameter lets you bypass &lt;code&gt;attr_accessible&lt;/code&gt; and &lt;code&gt;attr_protected&lt;/code&gt; exactly as we'd like. But how do we call this? Well, you use &lt;code&gt;send&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;@model.send :attributes=, attributes, false&lt;/code&gt;&lt;/pre&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/6/25/how-to-bypass-attr_accessible-and-attr_protected-in-rails</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-04-20:15</id>
    <published>2008-04-20T21:42:00Z</published>
    <updated>2008-04-20T21:48:34Z</updated>
    <category term="rails" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/274282950/contributing-to-rails" rel="alternate" type="text/html" />
    <title>Contributing to Rails</title>
<content type="html">
            &lt;p&gt;Encouraged by &lt;a href="http://deaddeadgood.com/2008/4/13/conditional-page-caching-in-rails#comments"&gt;a couple of kind comments&lt;/a&gt; &lt;a href="http://rails.lighthouseapp.com/projects/8994/tickets/25-allow-if-option-for-caches_page"&gt;I submitted a patch&lt;/a&gt; for the change I wrote about in my &lt;a href="http://deaddeadgood.com/2008/4/13/conditional-page-caching-in-rails"&gt;previous post&lt;/a&gt;, and &lt;a href="http://github.com/rails/rails/commit/14a40804a29a57ad05ca6bffbe1e5334089593a9"&gt;as of yesterday&lt;/a&gt; you can now do something like this in edge Rails.&lt;/p&gt;

&lt;pre&gt;
&lt;code class="ruby"&gt;caches_page :index, :if =&gt; Proc.new { |c| !c.request.format.json? }&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;I'd not used &lt;a href="http://lighthouseapp.com/"&gt;Lighthouse&lt;/a&gt; before and I'm still getting my head around everything &lt;a href="http://git.or.cz/"&gt;Git&lt;/a&gt;. If you're in a similar position, you might find these links useful too:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.tpope.net/rails-git-best-practices"&gt;Best Practices for Contributing to Rails with Git&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.thechrisoshow.com/2008/4/13/five-rails-tips/"&gt;Five tips for contributing to Rails&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rails.lighthouseapp.com/projects/8994/ticket-guidelines"&gt;Ticket Guidelines&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/4/20/contributing-to-rails</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-04-13:7</id>
    <published>2008-04-13T19:16:00Z</published>
    <updated>2008-04-20T21:59:22Z</updated>
    <category term="caching" />
    <category term="rails" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/269606296/conditional-page-caching-in-rails" rel="alternate" type="text/html" />
    <title>Conditional Page Caching in Rails</title>
<content type="html">
            &lt;p class="update"&gt;Update 20/04/2008: &lt;a href="http://deaddeadgood.com/2008/4/20/contributing-to-rails"&gt;I submitted a patch and this is now in edge Rails&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I &lt;a href="http://deaddeadgood.com/2008/3/27/really-simple-blog-badges-in-rails-2-0"&gt;created the JSON feed for my deadtre.es blog badge&lt;/a&gt;, one thing I didn't take care of was caching. As the contents of the feed will always be the same for a given URL no matter which user makes the request, it's a prime candidate for page caching. All I need to do is add &lt;code&gt;caches_page :index&lt;/code&gt; to my controller, and Rails takes care of caching the index action for me. However, there's a problem here as the HTML format index action will also be cached, and unlike the JSON feed this does vary based on the logged on user (if you're looking at your own bookmarks you get edit and delete links for example) and so it isn't as suitable for page caching. What I'd really like to do here is to specify a condition that will be evaluated before the page is cached, much as the &lt;a href="http://www.noobkit.com/show/ruby/rails/rails-edge/actionpack-edge/actioncontroller/sessionmanagement/classmethods/session.html"&gt;session &lt;/a&gt; method does.&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;caches_page :index, :if =&gt; Proc.new { |c| c.request.format.json? }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unfortunately, a quick look at &lt;a href="http://www.noobkit.com/show/ruby/rails/rails-edge/actionpack-edge/actioncontroller/caching/pages/classmethods/caches_page.html"&gt;the docs for caches_page&lt;/a&gt; tells me that it doesn't support this. But looking at the source code I can see that &lt;code&gt;caches_page&lt;/code&gt; is really just a nice shortcut for setting up an &lt;code&gt;after_filter&lt;/code&gt; to perform the caching. So, instead of calling &lt;code&gt;caches_page&lt;/code&gt; I could do this directly myself, and check the format of the page requested so that only the JSON feed is cached:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;after_filter(:only =&gt; :index) { |c| c.cache_page if c.request.format.json? }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, although this works, it doesn't check that caching is enable (as &lt;code&gt;caches_page&lt;/code&gt; does) and it's also not that pretty. Instead, I decided to override &lt;code&gt;caches_page&lt;/code&gt; by dropping the following in my ApplicationController.&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;def self.caches_page(*actions)
  return unless perform_caching
  options = actions.extract_options!
  after_filter(:only =&gt; actions) { |c| c.cache_page if options[:if].nil? or options[:if].call(c) }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is pretty similar to the original &lt;code&gt;caches_page&lt;/code&gt; except that it evaluates any &lt;code&gt;:if&lt;/code&gt; proc passed in before caching the page. Now, I can now call &lt;code&gt;caches_page&lt;/code&gt; with the syntax I originally wanted:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;caches_page :index, :if =&gt; Proc.new { |c| c.request.format.json? }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Job done.&lt;/p&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/4/13/conditional-page-caching-in-rails</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-04-02:4</id>
    <published>2008-04-02T21:12:00Z</published>
    <updated>2008-04-02T21:14:01Z</updated>
    <category term="git" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/262923803/building-git-on-ubuntu-6-06" rel="alternate" type="text/html" />
    <title>Building Git on Ubuntu 6.06</title>
<content type="html">
            &lt;p&gt;I'm spending some time updating my Rails projects to use &lt;a href="http://www.capify.org/"&gt;Capistrano 2&lt;/a&gt; and &lt;a href="http://git.or.cz/"&gt;Git&lt;/a&gt;. Here are the steps I took to build &lt;a href="http://git.or.cz/"&gt;Git&lt;/a&gt; on my Ubuntu 6.06 slice.&lt;/p&gt;

&lt;pre&gt;
&lt;code&gt;
sudo apt-get install curl libcurl3-gnutls-dev libexpat1-dev
wget http://kernel.org/pub/software/scm/git/git-1.5.4.5.tar.gz
tar xvf git-1.5.4.5.tar.gz
cd git-1.5.4.5.tar.gz
NO_TCLTK=yes make prefix=/usr/local all
NO_TCLTK=yes sudo make prefix=/usr/local install
&lt;/code&gt;
&lt;/pre&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/4/2/building-git-on-ubuntu-6-06</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-03-27:3</id>
    <published>2008-03-27T20:34:00Z</published>
    <updated>2008-06-29T18:52:41Z</updated>
    <category term="json" />
    <category term="rails" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/259239366/really-simple-blog-badges-in-rails-2-0" rel="alternate" type="text/html" />
    <title>Really Simple Blog Badges in Rails 2.0</title>
<content type="html">
            &lt;p&gt;When I wanted a quick (&lt;a href="http://ajaxian.com/archives/is-easy-implementation-the-same-as-good-code"&gt;and arguably dirty&lt;/a&gt;) way to show my current reading list from &lt;a href="http://deadtre.es"&gt;deadtre.es&lt;/a&gt; on this blog, I figured that it was time to play with &lt;a href="http://blog.codefront.net/2007/10/10/new-on-edge-rails-json-serialization-of-activerecord-objects-reaches-maturity/"&gt;the JSON support which was baked in Rails 2.0&lt;/a&gt;. Wanting to keep things super simple, all I wanted to do was:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Create a simple JSON feed I can include via a &lt;code&gt;&amp;lt;script&gt;&lt;/code&gt; tag. (I'll write a simple Javascript callback which will insert my reading list into each page. If I decide to make this easier for users I'll put together the usual configuration page where people can play with the look and feel of their badge and get some code to copy and paste.)&lt;/li&gt;
&lt;li&gt;Keep things RESTful. Each user already has a URL for their bookmarks so I really wanted to avoid to avoid creating new controllers or actions.&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;Cutting to the chase, this pretty much turns out to be a one-liner in Rails. Here's an example:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
class BookmarksController &amp;lt; ApplicationController
  session :off, :only =&gt; :index, :if =&gt; Proc.new { |request| request.format.json? }
  
  def index
    # fetch bookmarks ...
    respond_to do |format|
      format.json do     
        excluded_book_attrs = [:aws_domain, :id, :created_at, :updated_at]        
        excluded_bookmark_attrs = [:book_id, :id, :updated_at, :user_id]
        render :json =&gt; @bookmarks.to_json(:except =&gt; excluded_bookmark_attrs, :include =&gt; { :book =&gt; { :except =&gt; excluded_book_attrs }}), :callback =&gt; 'show_deadtrees_books'
      end
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It doesn't get much simpler than that! There are a couple of things worth pointing out:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;code&gt;render :json&lt;/code&gt; takes care of setting the content type to application/json in the reponse header. Additionally, it wraps the JSON in a call to the method specified by the &lt;code&gt;:callback&lt;/code&gt; option. (I should allow users to override this via the querystring to avoid name conflicts in their Javascript or to remove the callback altogether to support use outside of a &lt;code&gt;&amp;lt;script&gt;&lt;/code&gt; tag.)&lt;/li&gt;

&lt;li&gt;You can include Active Record associations when calling &lt;code&gt;to_json&lt;/code&gt; using &lt;code&gt;:include&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;You can also control which attributes are serialized (including those on associations) using the &lt;code&gt;:only&lt;/code&gt; and &lt;code&gt;:except&lt;/code&gt; options.&lt;/li&gt;

&lt;li&gt;I've disabled sessions for JSON requests to the index action since they're not required and carry a reasonable overhead.&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;With that in place, all you need to do is implement your Javascript callback (&lt;a href="/javascripts/custom.js"&gt;borrow mine&lt;/a&gt; if you like) and your blog badge is done!&lt;/p&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/3/27/really-simple-blog-badges-in-rails-2-0</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-03-16:2</id>
    <published>2008-03-16T20:58:00Z</published>
    <updated>2008-03-16T20:59:41Z</updated>
    <category term="deadtrees" />
    <category term="projects" />
    <category term="rails" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/252642695/introducing-deadtrees" rel="alternate" type="text/html" />
    <title>Introducing deadtre.es</title>
<content type="html">
            &lt;p&gt;I suppose the laziest way to describe &lt;a href="http://deadtre.es/"&gt;deadtre.es&lt;/a&gt; is as '&lt;a href="http://del.icio.us/"&gt;del.icio.us&lt;/a&gt; for books'. I've been tinkering with it on and off over the last couple of months or so, and thought it time I polished off the roughest edges and gave it a mini-launch here. As far as functionality goes it's missing some fairly obvious stuff at the minute, but it's usable and includes a bookmarklet for adding books from the pages of &lt;a href="http://www.amazon.co.uk/"&gt;Amazon&lt;/a&gt; and a simple &lt;a href="http://www.json.org/"&gt;JSON&lt;/a&gt; feed should you want to display your reading list on your blog. If you want something a little more comprehensive you might want to check out &lt;a href="http://www.librarything.com"&gt;Library Thing&lt;/a&gt;, &lt;a href="http://www.bookjetty.com"&gt;BookJetty&lt;/a&gt; or maybe &lt;a href="http://www.amazon.co.uk/exec/obidos/wishlist/"&gt;Amazon Wish Lists&lt;/a&gt;, but if you want a super simple way to keep and share a reading list you're more than welcome to give &lt;a href="http://deadtre.es/"&gt;deadtre.es&lt;/a&gt; a go.&lt;/p&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/3/16/introducing-deadtrees</feedburner:origLink></entry>
  <entry xml:base="http://deaddeadgood.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:deaddeadgood.com,2008-03-11:1</id>
    <published>2008-03-11T23:04:00Z</published>
    <updated>2008-06-29T18:53:51Z</updated>
    <category term="acts_as_taggable_on_steroids" />
    <category term="rails" />
    <category term="will_paginate" />
    <link href="http://feeds.feedburner.com/~r/deaddeadgood/~3/249785991/will_paginate-and-acts_as_taggable_on_steroids" rel="alternate" type="text/html" />
    <title>will_paginate and acts_as_taggable_on_steroids</title>
<content type="html">
            &lt;p&gt;Getting the &lt;a href="http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids"&gt;acts_as_taggable_on_steroids&lt;/a&gt; and &lt;a href="http://rock.errtheblog.com/will_paginate"&gt;will_paginate&lt;/a&gt; plugins playing nicely together has been written about plenty of times before, but I still ended up doing my own thing when I tackled this on a project of mine. This topic came up in conversation again recently so I guess it's time I wrote up my solution.&lt;/p&gt;

&lt;p&gt;Using the typical blog application as an example you might have a Post model which &lt;code&gt;acts_as_taggable&lt;/code&gt;. Together, the plugins give you a &lt;code&gt;Post.paginate_tagged_with&lt;/code&gt; method which looks as though it will do what you want, but unfortunately doesn't work as expected. You may well get the correct results, but the SQL that is generated to count the total number of results is incorrect so you'll see some strange behavior in you paging controls.&lt;/p&gt;

&lt;p&gt;The solution I settled on, was to create a custom finder (paginater?) on the Post model. It looks like this:&lt;/p&gt;

&lt;pre&gt;
&lt;code class="ruby"&gt;
class Post &amp;lt; ActiveRecord::Base
  acts_as_taggable

  def self.paged_find_tagged_with(tags, args = {})
    if tags.blank?
      paginate args
    else
      options = find_options_for_find_tagged_with(tags, :match_all =&gt; true)
      options.merge!(args)
      # The default count query generated by paginate includes COUNT(DISTINCT Posts.*) which errors, at least on mysql
      # Below we override the default select statement used to perform the count so that it becomes COUNT(DISTINCT Posts.id)
      paginate(options.merge(:count =&gt; { :select =&gt; options[:select].gsub('*', 'id') }))
    end
  end
end
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;You use it as you would the regular &lt;code&gt;paginate_tagged_with&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;@posts = Post.paged_find_tagged_with('rails', :page =&gt; 1)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The way this is works is that we first call &lt;code&gt;find_options_for_find_tagged_with&lt;/code&gt; to get a hash of options required to perform a find by tag. If we hand this straight to &lt;code&gt;paginate&lt;/code&gt; we'll get an error trying to &lt;code&gt;SELECT COUNT(DISTINCT Posts.*)&lt;/code&gt;. Thankfully however, will_paginate lets you override the options it uses to generate its count SQL. If we take &lt;code&gt;options[:select]&lt;/code&gt; and replace the '*' with 'id' and use that as the &lt;code&gt;:select&lt;/code&gt; for our &lt;code&gt;:count&lt;/code&gt;, the SQL generated is &lt;code&gt;SELECT COUNT(DISTINCT Posts.id)&lt;/code&gt; and everything works as expected.&lt;/p&gt;

&lt;p&gt;That's it, really. It's worth pointing out that when no tags are passed in I'm calling &lt;code&gt;paginate&lt;/code&gt; (without any additional options) to return all posts. This makes sense in my app and moves some logic out of the controller but you might want to do something different.&lt;/p&gt;
          </content>  <feedburner:origLink>http://deaddeadgood.com/2008/3/11/will_paginate-and-acts_as_taggable_on_steroids</feedburner:origLink></entry>
</feed>
