ColumnScope Plugin version 1.0 released!
* <http://github.com/boof/column_scope/>
ColumnScope lets you select specified values from your DB into a model or an array without doing :select => SQL_FRAGMENT and map! {|r|r.foo} all the time. This plugin extends ActiveRecord::Base and mixes in named scopes. ColumnScope is a subclass of ActiveRecord::NamedScope::Scope so it can be chained with named scopes.
Assume we have a Page(id:integer, path:string, title:string, body:text, timestamps) model and we have a page listing, there is no need to load the whole body for every page. Maybe we need only the path and title:
Page.selects(:path, :title).values.all(:order => 'id') # => [['/foo', 'Foo'], ['/bar', 'Bar'], ...]
Further we can chain it with named scopes:
Page.published.selects(:path, :title).values.all(:order => 'id') # => [['/foo', 'Foo'], ['/baz', 'Baz'], ...] # or using a shortcut Page.published.select_all(:path__title, :order => 'id') # => [['/foo', 'Foo'], ['/baz', 'Baz'], ...]
When this page is shown maybe we don't need the path and id anymore, but the instance of the model:
Page.published.rejects(:id, :path).find params[:id] # => <Page title="Foo" ...>