Select products, that have all of the parameters in column

71 Views Asked by At

I have a Product model and ProductParams, where I store product ids, column with some additional parameters(param) and param type. It will look like that:

id | type  | param
   |       |
1  | color | blue
1  | type  | hard
2  | color | blue
2  | type  | soft
.. | ..... | .....

How do I get only first product, when I call it with params = [blue, hard]?
For now I thought of something like Product.joins{product_params}.where{product_params.param.in params}.uniq, but I get 2nd as well, because it is blue too...

I use squeel gem, to constuct SQL queries, but answer doesn't have to be in squeel as well.

EDIT
I came up with solution, but it's a bit strange:

Product.joins{product_params}.where{product_params.param.in params}.group{id}.having{count(product_params.param) == 2}

But it would be great, if there was more graceful one.

0

There are 0 best solutions below