has_many :through + polymorphic relationships

后端 未结 1 2030
旧时难觅i
旧时难觅i 2021-02-04 21:09

I using rails3 and trying to build some complex associations.

I have Product, Version and Property models.

class Version < ActiveRecord::Base
  belon         


        
1条回答
  •  北海茫月
    2021-02-04 21:29

    How about:

    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 讨论(0)
提交回复
热议问题