Skip to content

Instantly share code, notes, and snippets.

@oleksiilevzhynskyi
Created September 9, 2011 11:39
Show Gist options
  • Save oleksiilevzhynskyi/1205996 to your computer and use it in GitHub Desktop.
Save oleksiilevzhynskyi/1205996 to your computer and use it in GitHub Desktop.
has_many through polymorfic
class Widget < ActiveRecord::Base
has_many :widget_groupings
has_many :people, :through => :widget_groupings, :source => :grouper, :source_type => 'Person'
has_many :aliens, :through => :widget_groupings, :source => :grouper, :source_type => 'Alien'
end
class Person < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class Alien < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class WidgetGrouping < ActiveRecord::Base
belongs_to :widget
belongs_to :grouper, :polymorphic => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment