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 = Array.new
  @columns_hash = { "type" => :fake }


  ...
end

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s