を使用すると、複数のプロパティでフィルタすることができますいくつかのコードです。理想的ではありませんが(適切な検証などはありませんが)、複数の「in」サブクエリを実行するよりも優れていると思います。
def add_search_scopes(base_scope)
joins = nil
conditions = nil
product_property_alias = nil
i = 1
search.each do |name, scope_attribute|
scope_name = name.to_sym
# If method is defined in product_filters
if base_scope.respond_to?(:search_scopes) && base_scope.search_scopes.include?(scope_name.to_sym)
base_scope = base_scope.send(scope_name, *scope_attribute)
else
next if scope_attribute.first.empty?
# Find property by name
property_name = name.gsub('_any', '').gsub('selective_', '')
property = Spree::Property.find_by_name(property_name)
next unless property
# Table joins
joins = product if joins.nil?
product_property_alias = product_property.alias("filter_product_property_#{i}")
joins = joins.join(product_property_alias).on(product[:id].eq(product_property_alias[:product_id]))
i += 1
# Conditions
condition = product_property_alias[:property_id].eq(property.id)
.and(product_property_alias[:value].eq(scope_attribute))
conditions = conditions.nil? ? condition : conditions.and(condition)
end
end if search.is_a?(Hash)
joins ? base_scope.joins(joins.join_sources).where(conditions) : base_scope
end
def prepare(params)
super
@properties[:product] = Spree::Product.arel_table
@properties[:product_property] = Spree::ProductProperty.arel_table
end
私はこの問題を自分で解決しました。今私はメタプログラミングによって動的フィルタを作成しています。実行時にフィルタを作成します。 –
どのように達成されたかのヒント/ – GorillaApe