4

Я использую rails3 и пытаюсь построить некоторые сложные ассоциации.has_many: через + полиморфные отношения

У меня есть модели продуктов, моделей и моделей.

class Version < ActiveRecord::Base 
    belongs_to :product 
    has_many :specs 
    has_many :properties, :through => :specs 
end 

class Product < ActiveRecord::Base 
    has_many :versions 
    has_many :specs 
    has_many :properties, :through => :specs 
end 

class Property < ActiveRecord::Base 
end 

class Spec < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :spec 
    belongs_to :version 
end 

Он отлично работает, но я хочу использовать продукт и версию в качестве полиморфных отношений, поэтому табличные данные будут иметь только spec_id и some_other_id, вместо spec_id, product_id, version_id.

Я не могу понять, куда я должен положить: как и где: polymorphic => true. Вы можете мне помочь?

ответ

4

Как насчет:

class Version < ActiveRecord::Base 
    belongs_to :product 
    has_many :specs, :as => :speccable 
    has_many :properties, :through => :specs 
end 

class Product < ActiveRecord::Base 
    has_many :versions 
    has_many :specs, :as => :speccable 
    has_many :properties, :through => :specs 
end 

class Property < ActiveRecord::Base 
end 

class Spec < ActiveRecord::Base 
    belongs_to :speccable, :polymorphic => true 
    belongs_to :spec 
end 
#table: specs(id,spec_id,speccable_type,speccable_id) 
+0

Спасибо. Он работает, просто нужно было изменить адресность до точки. и нет «belongs_to: spec», но «принадлежит_от: свойство» (по моей вине) –

Смежные вопросы