2011-12-09 85 views
0

我有以下型号:双向polymophic关系

class Infoset < ActiveRecord::Base 
    belongs_to :infosetable, :polymorphic => true 
end 

class Product < ActiveRecord::Base 
    belongs_to :productset 
    has_one :infoset, :as => :infosetable, :dependent => :destroy 
end 

class Productset < ActiveRecord::Base 
    has_one :infoset, :as => :infosetable, :dependent => :destroy 
    has_many :products 
end 

我可以通过Product.infosetProductset.infoset

信息集细,但我怎么得到通过InfosetProductProductset

infoset.product只是返回信息。

回答

3

@infoset.infosetable将根据情况返回产品或产品组。

+0

我刚刚得到method_missing,当我尝试。 –

+0

等等...我想我有一个愚蠢的攻击。试了一遍,它似乎工作.. –

+0

我试图打电话infosetable集合。 DERP。 'infoset = Infoset.where(:usp_sku => params [:usp_sku])infoset.infosetable' –