Selecting only specified columns can safe you resources and processing time.
I wrote this plugin because I do this all the time and I don’t want to cripple my code anymore.
ColumnScope is build on top of NamedScope so it only works with Ruby on Rails 2.1+.
Item.selects(:name).proxy_options[:select] # => '"items"."name"' content = 'Foo Bar Baz' Item.create :name => 'foo', :value => 1, :content => content Item.create :name => 'bar', :value => 2, :content => content Item.create :name => 'baz', :value => 3, :content => content Item.selects(:name).values.all :order => 'id' # => %w[foo bar baz] # use a shortcut to retrieve values of name and content distinct Item.select_first :distinct_name__content # => ['foo', content] # without shortcut Item.selects(:name, :content).uniq!.values.first # => ['foo', content] # reject named columns in scope :a_named_scope and select only the values Item.a_named_scope.rejects(:content).values.all # => [[1, 'foo', 1], ...]
See test/column_scope_test.rb for more examples.
$ script/plugin install git://github.com/boof/column_scope.git # alternativly: $ cd vendor/plugins $ git clone git://github.com/boof/column_scope.git
Copyright © 2008 Florian Aßmann, released under the MIT license