Skip to content

Instantly share code, notes, and snippets.

@damncabbage
Created January 26, 2012 23:46
Show Gist options
  • Save damncabbage/1685896 to your computer and use it in GitHub Desktop.
Save damncabbage/1685896 to your computer and use it in GitHub Desktop.
Retrieving non-AR columns with an AR query
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