I needed to create a query with Rails, something similar to this:

filters = Filter.joins(:category_filters).where("category_id IN (?)", params[:categories]).group("filters.id")

And also the MySQL statement that's making is:

SELECT `filters`.* FROM `filters` INNER JOIN `category_filters` ON `category_filters`.`filter_id` = `filters`.`id` WHERE (category_id IN ('9,4')) GROUP BY filters.id

In the beginning sight, this question is alright, however when I look the outcomes, its wrong. Allow me to explain you.

First, this can be a query towards the table filters:

select * from filters;

+----+----------+-------+----------+------------+------------+
| id | name     | other | optional | created_at | updated_at |
+----+----------+-------+----------+------------+------------+
|  1 | material |     1 |        1 | NULL       | NULL       |
|  2 | abc      |     1 |        0 | NULL       | NULL       |
|  3 | xyz      |     0 |        0 | NULL       | NULL       |
|  4 | 123a     |     0 |        0 | NULL       | NULL       |
+----+----------+-------+----------+------------+------------+

Second, this can be a query towards the table category_filters:

select * from category_filters;

+----+-----------+-------------+------------+------------+
| id | filter_id | category_id | created_at | updated_at |
+----+-----------+-------------+------------+------------+
|  1 |         1 |           1 | NULL       | NULL       |
|  2 |         2 |           1 | NULL       | NULL       |
|  3 |         1 |           9 | NULL       | NULL       |
|  4 |         2 |           9 | NULL       | NULL       |
|  5 |         1 |           4 | NULL       | NULL       |
|  6 |         3 |           4 | NULL       | NULL       |
+----+-----------+-------------+------------+------------+

And today, the query produced by Rails (the very first query):

SELECT `filters`.* FROM `filters` INNER JOIN `category_filters` ON `category_filters`.`filter_id` = `filters`.`id` WHERE (category_id IN ('9,4')) GROUP BY filters.id;

+----+----------+-------+----------+------------+------------+
| id | name     | other | optional | created_at | updated_at |
+----+----------+-------+----------+------------+------------+
|  1 | material |     1 |        1 | NULL       | NULL       |
|  2 | abc      |     1 |        0 | NULL       | NULL       |
+----+----------+-------+----------+------------+------------+

Why happening?

However, this really is similar query, rather than using IN I made use of OR, such as this:

SELECT `filters`.* FROM `filters` INNER JOIN `category_filters` ON `category_filters`.`filter_id` = `filters`.`id` WHERE (category_filters.category_id=9 or category_filters.category_id=4) GROUP BY filters.id;

+----+----------+-------+----------+------------+------------+
| id | name     | other | optional | created_at | updated_at |
+----+----------+-------+----------+------------+------------+
|  1 | material |     1 |        1 | NULL       | NULL       |
|  2 | abc      |     1 |        0 | NULL       | NULL       |
|  3 | xyz      |     0 |        0 | NULL       | NULL       |
+----+----------+-------+----------+------------+------------+

What's happening?

Inside your WHERE clause, do this (presuming category_id is really a number):

category_id IN (9,4)

else this (presuming category_id is really a string)

category_id IN ('9','4')

rather than this (inside your original query)

category_id IN ('9,4')

Should you simply do

where(:category_id => params[:categories]

Rails can create the correct SQL syntax for you personally