Ich habe eine Rails App, in der ich Produkte und ihre Varianten habe.Schienen holen Produkte basierend auf Minimal- und Maximalpreis
product.rb
class Product < ActiveRecord::Base
has_many :variants
scope :with_min_range, lambda { |min|
where(" (variant_price) >= ?", "#{min.to_i}")
}
scope :with_max_range, lambda { |max|
where("(variant_price) <= ?", ["#{max.to_i}"])
}
def price_range
return @price_range if @price_range
return @price_range = ['N/A', 'N/A'] if active_variants.empty?
@price_range = active_variants.minmax {|a,b| a.price <=> b.price }.map(&:price)
end
def price_range?
!(price_range.first == price_range.last)
end
end
die Art, wie ich die Preisspanne des Produkts holen ist
index.html.erb
<% @products.each do |product| %>
<figcaption>
<h4 class="aa-product-title"><%= link_to product.name, product_path(product) %></h4>
<span class="aa-product-price"><%= number_to_currency(product.price_range.first, :locale => :in) %>
<% if product.price_range? %>
to
<%= number_to_currency(product.price_range.last, :locale => :in) %>
<% end %></span>
</figcaption>
<% end %>
Jetzt können Sie sehen in die product.rb Ich möchte das Produkt basierend auf dem Preis, so dass in der with_min_range
das Ergebnis w abholen be ill die Produkte, deren Varianten Mindestpreisspanne wird größer sein als der Wert von min
und in der with_max_range
wird das Ergebnis die Produkte, deren Varianten Höchstpreis wäre weniger als die max
PS - ich habe variant_price
in der where Abfrage gegeben nur Sie Idee zu geben, was ich
wollen Sie mir bitte helfen, seine Lösung, um herauszufinden,