V
V
Viktor Vsk2014-09-12 10:44:04
Ruby on Rails
Viktor Vsk, 2014-09-12 10:44:04

Is it possible to group a query by OR in WHERE using Ransack?

Product.search(product_properties_property_id_eq: 'x', product_propertries_value_eq: 'y')

Ransack generates a request with something like:
WHERE (("product_properties"."value" = 'x' AND "product_properties"."property_id" = 'y'))

With the help of a combinator, you can change this AND to OR, yes:
WHERE (("product_properties"."value" = 'x' OR "product_properties"."property_id" = 'y'))

But is it possible with ransack to make a query like:
WHERE(
( product_properties.value = 'x' AND product_properties.property_id = 'y' ) OR
( product_properties.value = 'x1' AND product_properties.property_id = 'y1' ) OR
( product_properties.value = 'x2' AND product_properties.property_id = 'y2' ) OR
)

Meaning - the usual filter of goods. If this is implemented in Spree or ror-e, please poke, because. everything that I saw there was a slightly different plan, as it seemed to me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Bakin, 2014-10-30
@recomp

why do you need OR or Ransack for this? use scopes in the model.

def self.properties( properties ) # properties = [1, 2, 3]
    if properties.present?
      if properties.first == 'all'  # я делал себе для выборки товаров со всеми параметрами
         properties = Product_property.all.pluck(:property_id)
         where(property_id: properties)
      else
        where(property_id: properties) # WHERE .property_id IN (value1,value2,...);
      end
    end
  end

then use:
Product.properties(params[:properties])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question