Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A simple column reader for ActiveRecord
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
gemfiles
lib
spec
.gitignore
.travis.yml
Gemfile
README
Rakefile
activerecord-column-reader.gemspec

README

A very simple utility, for when you want to easily read only a single column from an ActiveRecord model.

Usage:

    class Monster < ActiveRecord::Base
      column_reader :name
      column_reader :hairyness, :as => :hirsutenesses
      column_reader :dob

      scope :dangerous, :conditions => {:carnivorous => true}
    end

At it's simplest, it returns the values of just the given column:

    Monster.names
    # => ['Graham', 'Mike', 'Fuzzly']

The default accessor is the column name pluralized, but using the :as option, another name can be chosen:

    Monster.hirsutenesses
    # => ['bald', 'very', 'very']

Scopes are respected:

    Monster.dangerous.names
    # => ['Fuzzly']

And columns are correctly type cast:

    Monster.dobs:
    # => [#<Date: 4886491/2,0,2299161>, #<Date: 2886491/2,0,2299161>, #<Date: 3886491/2,0,2299161>]
Something went wrong with that request. Please try again.