Skip to content

Instantly share code, notes, and snippets.

@radar
Created August 8, 2008 00:52
Show Gist options
  • Save radar/4527 to your computer and use it in GitHub Desktop.
Save radar/4527 to your computer and use it in GitHub Desktop.
class Address < ActiveRecord::Base
belongs_to :suburb
belongs_to :thing, :polymorphic => true
end
Fields:
thing_id
thing_type
suburb_id
class Client < ActiveRecord::Base
has_one :address, :as => :thing, :dependent => :destroy
end
Fields:
name
class Employer < ActiveRecord::Base
has_one :address, :as => :thing, :dependent => :destroy
end
Fields:
name
Finding a suburb with a postcode of "5114", and getting all the employers.
class Suburb < ActiveRecord::Base
has_many :addresses
end
Fields:
id
name
postcode
SQL statement:
SELECT employers.* FROM `suburbs`, `addresses`, `employers` WHERE suburbs.postcode = "5114" AND suburbs.id = addresses.suburb_id AND addresses.thing_id = employers.id and addresses.thing_type = 'employer';
Oops, could call this with #find_by_sql and it does exactly as it should. Yuji's the man.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment