0

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.

Artyom Tsoy
  • 2,238
  • 1
  • 13
  • 18
  • I'm a bit confused by your question: can you describe your situation in more detail in the question, eg with proper model names? When you start using example names instead of the real ones it can cause a lot of confusion, so if you're going to use example model, column etc names then at least spell them all out in detail. – Max Williams Dec 06 '13 at 10:46
  • Well, I don't really know what to add here. I have a form, that submits params, and when they are submitted, I need to get Products, that are meeting these params. – Artyom Tsoy Dec 06 '13 at 11:00

0 Answers0