#15
Apr 06
Fun with Find Conditions
You can pass more than simple strings to find conditions. Arrays, ranges, and nil values can be passed as well. In this episode you will see the tricks involved with passing these odd objects to find conditions. (Update: audio fixed).
Task.find(:all, :conditions => ["complete=? and priority=?", false, 3]) Task.find(:all, :conditions => ["complete=? and priority IS ?", false, nil]) Task.find(:all, :conditions => ["complete=? and priority IN (?)", false, [1,3]]) Task.find(:all, :conditions => ["complete=? and priority IN (?)", false, 1..3]) Task.find(:all, :conditions => { :complete => false, :priority => 1 }) Task.find(:all, :conditions => { :complete => false, :priority => nil }) Task.find(:all, :conditions => { :complete => false, :priority => [1,3] }) Task.find(:all, :conditions => { :complete => false, :priority => 1..3 }) Task.find_all_by_priority(1..3)




WOOW ! I've been looking for this for days !! Thanks a lot !
Any idea how to specify exclusion criteria? For example, what is the syntax for excluding records with priority 1 & 3 using the hash for input.
@Bob, I don't think that's possible. You'll have to use a string for the conditions.
Thanks Ryan, that's what I thought, so I should specify "priority NOT IN (?)", in the string.
Hi, how do you combine this with eagerLoading?
Task.find(:all, :include => projects, :conditions => ["tasks.complete=? and tasks.priority = ?", false, nil])
Does this work?
tHnx