Getting several AR objects in one query: ActiveRecord::Content::Inquirer

There are some issues already included in Station I want to blog, because I think they are interesting.
One of them is ActiveRecord::Content::Inquirer

In Station, a container is an ActiveRecord model that have many contents, like posts, images, videos, bookmarks, etc.. What if you want to obtain all the contents in a Container using just one SQL query? And you also want to paginate them? And, of course, getting back instances of every Content!

ActiveRecord::Content::Inquirer does just this. You use it:

ActiveRecord::Content::Inquirer.all :container => somespace,
                                    :page => params[:page],
                                    :per_page => 10

It even accepts an Array in :container.

Some of the magic is under this hack:

class Inquirer < ActiveRecord::Base
  @colums =
  @columns_hash = { "type" => :fake }


This way, it’s possible to use ActiveRecord::Base goodies without a real table in the database.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s