Проблема с Полиморфным has_many - PullRequest
0 голосов
/ 23 июня 2011

У меня есть следующая таблица «участия»:

id: integer
coupon_id: integer
participant_type: string
participant_id: integer

и у меня есть следующие модели:

class Participation < ActiveRecord::Base
  belongs_to :coupon

  belongs_to :participant, :polymorphic => true

  belongs_to :group, :class_name => "Group", 
                     :foreign_key => "participant_id"
  belongs_to :location, :class_name => "Location", 
                        :foreign_key => "participant_id"
end


class Coupon < ActiveRecord::Base
  has_many        :partipations, :as => :participant
  has_many        :groups, :through => :participations, :source => :group, 
                  :conditions => "participants_type = 'Group'"
  has_many        :locations, :through => :participations, :source => :location, 
                  :conditions => "participants_type = 'Location'"
end

Это указано в статье здесь и также похож на документацию ActiveRecord здесь .Он с треском проваливается при доступе к Coupon.first.groups или Coupon.first.locations с ошибкой:

ActiveRecord::HasManyThroughAssociationNotFoundError: 
Could not find the association :participations in model Coupon

Я пробовал другие варианты, но безуспешно.И, конечно же, доступ к Group.first.participations приводит к ошибке «неопределенный метод».

...