Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Multi select form & DB search

General • Asked by Sascha M.

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, 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:

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 29,763+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.