Multi select form & DB search
Hi there,
I want to allow a user to select multiple brands and teams in a select form field. I am using the user's input to create CSV through running a query.
So far I only can query single teams and brands:
...
Order.where("created_at between (?) and (?) and brand = ? and team = ?", self.start, self.end, self.brand, self.team).each do |order|
...
Do you know how to solve this?
Hey Sacha,
For those, you actually want to querying using IN
so that SQL can look for matches in an array of IDs. For example:
brand_ids = [1,2,3] # These IDs may come from your form params from a multi-select
Order.where(brand_id: brands)
# SELECT "orders".* FROM "orders" WHERE "orders"."brand_id" IN (1, 2, 3)
And remember you can chain your where
calls to make it cleaner:
Order.where("created_at between (?) and (?)", self.start, self.end).where(brand: self.brand, team: self.team)