Skip to content

Instantly share code, notes, and snippets.

@elskwid
Created January 16, 2010 00:19
Show Gist options
  • Save elskwid/278538 to your computer and use it in GitHub Desktop.
Save elskwid/278538 to your computer and use it in GitHub Desktop.
class Search < ActiveRecord::Base
has_many :search_results
has_many :cars, :through => :search_results, :source => :results, :source_type => "Car"
has_many :dealers, :through => :search_results, :source => :results, :source_type => "Dealer"
# search.results gives you the list of linked records
has_many :results, :class_name => "SearchResult"
end
class SearchResult < ActiveRecord::Base
belongs_to :search
# can be polymorphically linked to anything that is a resultant
belongs_to :results, :polymorphic => true
end
class Car < ActiveRecord::Base
# cars can be results
has_many :search_results, :as => :results
# -> car.searches gets you the list of searches
has_many :searches, :through => :search_results
end
class Dealer < ActiveRecord::Base
# dealers can be results
has_many :search_results, :as => :results
# -> dealer.searches gets you the list of searches
has_many :searches, :through => :search_results
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment