Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Better ActiveRecord finders

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 HISTORY.txt
Octocat-spinner-32 MIT-LICENSE.txt
Octocat-spinner-32 README.markdown
Octocat-spinner-32 Rakefile
Octocat-spinner-32 TODO
Octocat-spinner-32 better_ar.gemspec
README.markdown

BetterAr

BetterAr replaces the .all, .first and .count methods for ActiveRecord.

Install

Bundler:

gem 'better_ar'

Command line:

gem install better_ar

Usage

Now a single hash can be passed to ActiveRecord::Base.all that contains not only the where values but also other scopes like 'limit' and 'order' and also implicit joins

User.all(:age => 10, :limit! => 5, :offset! => 10, :order! => :name, :records => {:name => 'test'})

is the same as

User.joins(:records).where(:age => 10, :records => {:name => 'test'}).limit(5).offset(10).order(:name)

It also works on association collections:

User.first.records.all(:level => 2, :limit! => 5, :offset! => 10, :order! => :name, :reports => {:name => 'test'})

This would be the same as:

User.first.records.joins(:reports).where(:level => 2, :reports => {:name => 'test'}).limit(5).offset(10).order(:name)

While this may seem less concise the advantage is being able to dynamically construct the query with a single hash in code.

Legacy

If the params contain :condition it will fall back to the legacy method.

Other

Brian Cardarella (c) 2011

License: MIT (see MIT-LICENSE.txt)

Something went wrong with that request. Please try again.