Created
January 26, 2012 23:46
-
-
Save damncabbage/1685896 to your computer and use it in GitHub Desktop.
Retrieving non-AR columns with an AR query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Region < ActiveRecord::Base | |
acts_as_nested_set :left_column => 'left', :right_column => 'right' | |
attr_accessible :parent_id, :postcode, :name, :region_type | |
attr_accessor :area | |
def self.suburb_search(terms, options={}) | |
options = { | |
:limit => 20 | |
}.merge(options) | |
self.select('regions.id, regions.name, regions.postcode, a.name AS "area"') | |
.where(:region_type => 'suburb') | |
.where(['regions.name LIKE ?', "#{terms}%"]) | |
.joins('LEFT OUTER JOIN regions a ON a.id = regions.parent_id') | |
.limit(options[:limit]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment