Skip to content

Instantly share code, notes, and snippets.

@zxiest
Created January 11, 2012 22:07
Show Gist options
  • Save zxiest/1597048 to your computer and use it in GitHub Desktop.
Save zxiest/1597048 to your computer and use it in GitHub Desktop.
class Geolocation < ActiveRecord::Base
belongs_to :geotaggable, :polymorphic => true
end
class Location < ActiveRecord::Base
has_many :geolocations, :as => :geotaggable, :dependent => :destroy
end
irb(main):045:0> Location.first.geolocations
←[1m←[36mLocation Load (1.0ms)←[0m ←[1mSELECT `locations`.* FROM `locations`
LIMIT 1←[0m
←[1m←[35mGeolocation Load (1.0ms)←[0m SELECT `geolocations`.* FROM `geolocati
ons` WHERE `geolocations`.`geotaggable_id` = 1
irb(main):046:0> Location.find(:first, :include => :geolocations)
←[1m←[36mLocation Load (1.0ms)←[0m ←[1mSELECT `locations`.* FROM `locations`
LIMIT 1←[0m
←[1m←[35mGeolocation Load (1.0ms)←[0m SELECT `geolocations`.* FROM `geolocati
ons` WHERE `geolocations`.`geotaggable_type` = 'Location' AND (((`geolocations`.
`geotaggable_id` = 1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment