Skip to content
This repository

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…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 gemfiles
Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README
Octocat-spinner-32 Rakefile
Octocat-spinner-32 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.