Reputation: 3975
I want to find a record by doing something like this
@something = Model.where(X=1 OR Y=1)
But all the things I try don't work. This should be easy but I can't find the solution anywhere.
EDIT: To be more specific I am trying to find all records where X is 1 and Y is 1.
Upvotes: 0
Views: 724
Reputation: 9895
You may also benefit greatly from viewing the Advanced Queries in Rails 3 Railscast by Ryan Bates.
Upvotes: 2
Reputation: 3776
Which set here do you want:
| id | x | y |
| 1 | 1 | 0 |
| 2 | 0 | 1 |
| 3 | 1 | 1 |
If you want 3 only: Model.where(:x => 1, :y => 1)
If you want 1, 2, and 3:
tab=Model.arel_table; Model.where(tab[:x].eq(1).or(tab[:y].eq(1)))
Or even: Model.where(["x=? OR y=?",1,1])
if your OR statements aren't complicated.
Upvotes: 2
Reputation: 5714
You most likely want to use a where clause here on your model, i.e.
@something = Model.where('X=1 OR Y=1')
The stuff inside of the quotes is just SQL, so this would generate SQL that would look something like:
SELECT `model`.* FROM `model_table` WHERE (X = 1 or y = 1)
hope that helps!
ian.
Upvotes: 3