简体   繁体   中英

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. I can currently only get the search to work with one param with code below.

There is a has_many to has_many association between car model and colour_collection model.

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

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])

关于链接的更多讨论Rails ActiveRecord如何在没有多个查询的情况下链接“ where”子句?

Depending on whether you want to use OR 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
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= ? 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.

In Rails 4 :

@cars = car.
        joins(:colour_collections).
        where("colour_collections.name = ? or colour_collections.type = ?", params[:colour_collection], params[:type])

In 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]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM