I'm trying to find all records where two conditions are true. For example:
ruby-1.8.7-p302 > Person.all
=> #<Person name: "Jane", city: "Green Bay", state: "Wisconsin", single: true>
=> #<Person name: "Dick", city: "Madison", state: "Wisconsin", single: false>
=> #<Person name: "Tom", city: "Milwaukee", state: "Wisconsin", single: true>
I want to get the "Jane" and "Tom" records. I'm trying this, but it doesn't work:
Person.find_all_by_state("Wisconsin").find_all_by_single(true)
The SQL AND condition and OR condition can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition.
Example - Two Conditions in the WHERE Clause (AND Condition)You can use the AND condition in the WHERE clause to specify more than 1 condition that must be met for the record to be selected.
The SQL ALL Operator The ALL operator: returns a boolean value as a result. returns TRUE if ALL of the subquery values meet the condition.
You can use the AND and OR operators to combine two or more conditions into a compound condition. AND, OR, and a third operator, NOT, are logical operators. Logical operators, or Boolean operators, are operators designed to work with truth values: true, false, and unknown.
Example using a OR condition:
model_name.where("field_1 = ? OR field_2 = ?", params[:search_string], params[:search_string])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With