[英]Rails Query Multiple Params From Same Table
How can I search for multiple params? 如何搜索多个参数? I have checkboxes in my view, so if multiple checkboxes are selected, I would like all the
params
selected to be chosen. 我的视图中有复选框,因此如果选择了多个复选框,则希望选择所有已选择的
params
。 I can currently only get the search to work with one param
with code below. 我目前只能使搜索与下面的代码一起使用一个
param
。
There is a has_many
to has_many
association between car
model and colour_collection
model. car
模型和colour_collection
模型之间存在has_many
至has_many
关联。
Controller: 控制器:
@cars = car.joins(:colour_collections).where("colour_collections.name = ?", params[:colour_collection])
logs show this if two colours selected (eg red and green) creating duplicates in the resulting querie: 如果选择了两种颜色(例如红色和绿色),则日志显示此结果,从而在结果查询中创建重复项:
(0.7ms) SELECT COUNT(*) FROM "colour_collections"
ColourCollection Load (0.5ms) SELECT "colour_collections".* FROM "colour_collections"
Car Load (2.5ms) SELECT "cars".* FROM "cars" INNER JOIN "car_colour_collections" ON "car_colour_collections"."car_id" = "cars"."id" INNER JOIN "colour_collections" ON "colour_collections"."id" = "car_colour_collections"."colour_collection_id" WHERE "colour_collections"."name" IN ('Subtle', 'Intermediate') ORDER BY "cars"."created_at" DESC
CarAttachment Load (0.5ms) SELECT "car_attachments".* FROM "car_attachments" WHERE "car_attachments"."car_id" = $1 ORDER BY "car_attachments"."id" ASC LIMIT $2 [["car_id", 21], ["LIMIT", 1]]
CACHE (0.0ms) SELECT "car_attachments".* FROM "car_attachments" WHERE "car_attachments"."car_id" = $1 ORDER BY "car_attachments"."id" ASC LIMIT $2 [["car_id", 21], ["LIMIT", 1]]
CarAttachment Load (0.5ms) SELECT "car_attachments".* FROM "car_attachments" WHERE "car_attachments"."car_id" = $1 ORDER BY "car_attachments"."id" ASC LIMIT $2 [["car_id", 20], ["LIMIT", 1]]
CACHE (0.0ms) SELECT "car_attachments".* FROM "car_attachments" WHERE "car_attachments"."car_id" = $1 ORDER BY "car_attachments"."id" ASC LIMIT $2 [["car_id", 20], ["LIMIT", 1]]
If you want to search for multiple values in a single column for example 例如,如果要在单个列中搜索多个值
params[:colour_collection] = ['red','green','blue']
Then you would expect your query to look like this 然后,您希望查询看起来像这样
SELECT * FROM cars c
INNER JOIN colour_collections s
WHERE s.name IN ('red','green','blue');
In this case the corresponding ActiveRecord statement would look like this 在这种情况下,相应的ActiveRecord语句将如下所示
Car.
joins(:colour_collections).
where(colour_collections: { name: params[:colour_collection] })
@cars = car.joins(:colour_collections).where("colour_collections.name = ?", params[:colour_collection]).where("cars.make = ?", params[:make])
Depending on whether you want to use OR
or AND
. 取决于要使用
OR
还是AND
。 There are multiple ways of achieving this but simple example is 有多种方法可以实现此目的,但简单的示例是
Article.where(trashed: true).where(trashed: false)
the sql generated will be 生成的SQL将是
SELECT * FROM articles WHERE 'trashed' = 1 AND 'trashed' = 0
Foo.where(foo: 'bar').or.where(bar: 'bar')
This is norm in Rails 5 or simply Foo.where(foo: 'bar').or.where(bar: 'bar')
这在Rails 5中很常见
Foo.where('foo= ? OR bar= ?', 'bar', 'bar')
Rails 5 comes with an or
method but Rails 4 does not have the or
method, so you can use plain SQL query in Rails 4. Rails 5带有
or
方法,但是Rails 4没有or
方法,因此您可以在Rails 4中使用普通的SQL查询。
In Rails 4 : 在Rails 4中:
@cars = car.
joins(:colour_collections).
where("colour_collections.name = ? or colour_collections.type = ?", params[:colour_collection], params[:type])
In Rails 5 : 在Rails 5中:
@cars = car.
joins(:colour_collections).
where("colour_collections.name = ?", params[:colour_collection]).or(car.joins(:colour_collections).where("colour_collections.type = ?", params[:type]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.