0

Ich habe folgende DB Beziehungen in einer Rails-4-ProjektScopes trennen polymorphe Beziehungen

class Hat < ActiveRecord::Base 
    belongs_to :sale, as: product 
end 

class Shoe < ActiveRecord::Base 
    belongs_to :sale, as: product 
end 

class Sale < ActiveRecord::Base 
    has_many :products, polymorphic: true 

    scope :hats, -> { ??? } 
    scope :shoes, -> { ??? } 
end 

ein @sale Objekt gegeben, wie konnte ich alle bekommen die Produkte, die ein bestimmtes Modell gehören, wie Hat oder Shoe?

Antwort

1

Sie sollten eine product_type in Ihre Tabellen (the guides für weitere Informationen siehe), also würde ich eine Abfrage auf das tun:

scope :hats, -> { where(product_type: Hat.name) } 
# Not sure that this `Hat.class.name` is the actual value in the table, 
# so you might aswell check in your DB what's the correct value. 
+0

'Hat.class.name' macht' CLASS' –