本文共 1517 字,大约阅读时间需要 5 分钟。
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.count(:all, :conditions=>["complete=? and priority=? ", false, 3])
SELECT count(*) AS count_all FROM tasks WHERE (complete=0 and priority=3)
>>Task.count(:all, :conditions=>["complete=? and priority=? ", false, nil])
SELECT count(*) AS count_all FROM tasks WHERE (complete=0 and priority=NULL) >>Task.count(:all, :conditions=>["complete=? and priority IS ? ", false, nil])
>>Task.count(:all, :conditions=>["complete=? and priority in ? ", false, [1,3]])
SELECT count(*) AS count_all FROM tasks WHERE (complete=0 and priority IN (1,3))
>>Task.count(:all, :conditions=>["complete=? and priority in ? ", false, 1...3])
SELECT count(*) AS count_all FROM tasks WHERE (complete=0 and priority IN (0,1,2))
在rails1.2版本中,支持conditions适用hash:
>>Task.count(:all, :conditions=>{:complete=>false, :priority=>1})
>>Task.count(:all, :conditions=>{:complete=>false, :priority=>nil})
>>Task.count(:all, :conditions=>{:complete=>false, :priority=>[1,3]})
>>Task.count(:all, :conditions=>{:complete=>false, :priority=>1...3})
本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/131625,如需转载请自行联系原作者