ActiveRecord[五]:find()方法

上一篇 / 下一篇  2007-09-12 09:26:45 / 个人分类:rubyonrails学习笔记

1、:all找出所有符合条件的记录
2、:first找出符合条件的一条记录,不一定是应用程序创建的第一条记录
3、:conditions指定SQL语句中的where子句的查询条件,其值可以是一个SQL字符串;也可以是一个数组,其中包含带有占位符的SQL语句,以及各个占位符实际的值。如:
orders=Orders.find(:all,:conditions=> "name='Dave'")  #值为SQL字符串
name=params[:name]
orders=Orders.find(:all,:conditions=> ["name=?",name])  #值为数组,?是占位符,name是占位符的实际值
orders=Orders.find(:all,:conditions=> ["name=:name and pay_type=:pay_type",params])   #值为数组,:name和:pay_type都是占位符,params里有占位符的实际值,也可以把params写成{:name=>name,:pay_type=>pay_type}
3、:order指定排序条件,默认为升序,DESC为倒序,如:
orders=Order.find(:all,
                                  :conditions=>"name='Dave'",
                                  :order=>"pay_type,shipped_at DESC")  #按pay_type升序排列,按shipped_at倒序排列
4、:limit限制返回的记录数,如:
orders=Order.find(:all,
                                  :conditions=>"name='Dave'",
                                  :order=>"pay_type,shipped_at DESC"
                                  :limit=>10)   #返回符合条件的前十条记录
5、:offset总与:order同时使用,指定返回结果在整个结果集中的偏移量,就是从第几条记录开始取结果,如:
orders=Order.find(:all,
                                  :conditions=>"name='Dave'",
                                  :order=>"pay_type,shipped_at DESC"
                                  :limit=>10,
                                  :offset=>3)  #从符合条件的第三条记录开始取十条记录


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2009-01-08  
    123
45678910
11121314151617
18192021222324
25262728293031

数据统计

  • 访问量: 461
  • 日志数: 19
  • 建立时间: 2007-08-30
  • 更新时间: 2007-11-27

RSS订阅

Open Toolbar