ActiveRecord, has_many: through и полиморфные ассоциации
Folks,
Хочу убедиться, что я правильно понимаю это. И, пожалуйста, не обращайте внимания на случай наследования здесь (SentientBeing), пытаясь вместо этого сосредоточиться на полиморфных моделях в отношениях has_many: through. Тем не менее, рассмотрим следующее ...
class Widget < ActiveRecord::Base
has_many :widget_groupings
has_many :people, :through => :widget_groupings, :source => :person, :conditions => "widget_groupings.grouper_type = 'Person'"
has_many :aliens, :through => :widget_groupings, :source => :alien, :conditions => "video_groupings.grouper_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
В идеальном мире яЯ хотел бы, учитывая виджет и человека, сделать что-то вроде:
widget.people < my_person