Find file
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (27 sloc) 1.45 KB


Chainable APIs are great. There's no denying it. With Rails 3, ActiveRecord got a chainable API. But not so for ActiveResource, that red-headed stepchild of the Rails 'verse.


It's a gem. Install it.

$ gem install chaingang


First, include ChainGang in your ActiveResource::Base derived client class.

class Article < ActiveResource::Base = ''
  include ChainGang

Then, go wild:

# find all the articles /articles/published.xml?author=moonmaster9000&genre=sci-fi
@articles = Article.find(:all).from(:published)"moonmaster9000").and.genre?("sci-fi")

# at this point, @articles hasn't actually made the network call yet. 
@articles.each do |article| # now it's made the network call
  puts article.title

# or, find a specific article by id, but add a "?preview_data=true" onto the query string.
@article = Article.find('some-article-id').where.preview_data?(true)

# or, find the first article by moonmaster9000 and get the title; /articles.xml?author=moonmaster9000
@article = Article.find(:first)"moonmaster9000").title

# to specifiy the reqeust format, you can use format() method, which temporarily set Article.format for this request.
@article = Article.find(:first)"moonmaster9000").format(:json)         # or :xml