rails 4 how to use where and where in condition simultaneously
You could use arel, but I'd just do something like:
@posts = Post.where("category_id = ? AND product_model_id IN (?)", id, model)
model_ids = model.split(",").map(&:to_i)
@posts = Post.where(category_id: id, product_model_id: model_ids)
or
model_ids = model.split(",").map(&:to_i)
@posts = Post.where("category_id = ? AND product_model_id IN (?)", id, model_ids)
According to the rails guide, you can pass in an array to where
and it should understand that you want to use IN. See the guide here: http://guides.rubyonrails.org/active_record_querying.html#subset-conditions
I'm a little confused by your syntax, since model = (1, 2, 3, 4)
doesn't look like valid array syntax.
Relevant part of the guide:
Client.where(orders_count: [1,3,5])