Forum: Ruby on Rails [ANN] Plugin: ColumnScope 1.0

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Florian Aßmann (Guest)
on 2008-12-30 15:04
(Received via mailing list)
ColumnScope Plugin version 1.0 released!

* <>

ColumnScope lets you select specified values from your DB into a model
or an array without doing :select => SQL_FRAGMENT and map! {|r|}
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" ...>

Florian Aßmann
This topic is locked and can not be replied to.