Reputation: 1092
Lets say I have a Cat table and each cat has a unique color. Is there an activerecord method I can use to get the red and the blue cat in one query. My goal is a faster query than having to find each one individually with Cat.where(color: red) then doing the same thing with blue.
Upvotes: 2
Views: 3267
Reputation: 345
research JOIN
statement
you can also use OR
in where
where (color='blue' OR color='green')
check:
Join statement - toturialspoint.com
Upvotes: -1
Reputation: 3541
Cat.where(color: 'red').or.where(color: 'blue')
or
Cat.where("color = ? OR color = ?", "red", "blue")
or
Cat.where(color: ["red", "blue"])
Upvotes: 2
Reputation: 44685
You can do:
Cat.where(color: ['red', 'blue'])
this will generate SQL query:
SELECT cats.* FROM cats WHERE cats.color IN ('red', 'blue')
Upvotes: 7