现在的位置: 首页 > 综合 > 正文

Rails宝典之第十五式: find条件

2013年10月07日 ⁄ 综合 ⁄ 共 732字 ⁄ 字号 评论关闭

数据库查询的conditions除了简单的字符串,还可以用数组,range,nil等等,看看代码: 

Java代码  收藏代码
  1. Task.find(:all, :conditions => ["complete=? and priority=?"false3])  
  2. Task.find(:all, :conditions => ["complete=? and priority IS ?"false, nil])  
  3. Task.find(:all, :conditions => ["complete=? and priority IN (?)"false, [1,3]])  
  4. Task.find(:all, :conditions => ["complete=? and priority IN (?)"false1..3])  
  5. Task.find(:all, :conditions => { :complete => false, :priority => 1 })  
  6. Task.find(:all, :conditions => { :complete => false, :priority => nil })  
  7. Task.find(:all, :conditions => { :complete => false, :priority => [1,3] })  
  8. Task.find(:all, :conditions => { :complete => false, :priority => 1..3 })  
  9. Task.find_all_by_priority(1..3)  

find_by都可以使用数组、range、nil,确实不错。

【上篇】
【下篇】

抱歉!评论已关闭.