Active Record 查询
1.1 获取单个对象
1.1.1 使用主键
使用 Model.find(primary_key) 方法可以获取指定主键对应的对象。例如:
client = Client.find(10)
(注意:不同于find_by())
# => #<Client id: 10, first_name: "Ryan">
1.1.2 take
Model.take 方法会获取一个记录,不考虑任何顺序
Model.take 方法会获取一个记录,不考虑任何顺序client = Client.take# => #<Client id: 1, first_name: "Lifo">1.1.3 first
Model.first 获取按主键排序得到的第一个记录。例如:
client = Client.first# => #<Client id: 1, first_name: "Lifo">1.1.4 last
Model.last 获取按主键排序得到的最后一个记录。例如:
client = Client.last# => #<Client id: 221, first_name: "Russel">Model.last 如果没找到匹配的记录,不会抛出异常,而是返回 nil。
1.1.5 find_by
Model.find_by 获取满足条件的第一个记录。例如:
Client.find_by first_name: 'Lifo'
# => #<Client id: 1, first_name: "Lifo">1.1.6 take!
Model.take! 方法会获取一个记录,不考虑任何顺序。例如:
client = Client.take!# => #<Client id: 1, first_name: "Lifo">Model.take! 会抛出 ActiveRecord::RecordNotFound 异常。
first!,last!,find_by!
1.2 获取多个对象
1.2.1 使用多个主键
Model.find(array_of_primary_key) 方法可接受一个由主键组成的数组,返回一个由主键对应记录组成的数组。例如:
client = Client.find([1, 10]) # Or even Client.find(1, 10)
# => [#<Client id: 1, first_name: "Lifo">, #<Client id: 10, first_name: "Ryan">]1.2.2 take
Model.take(limit) 方法获取 limit 个记录,不考虑任何顺序:
Client.take(2)
# => [#<Client id: 1, first_name: "Lifo">, #<Client id: 2, first_name: "Raf">]
1.2.3 first
Model.first(limit) 方法获取按主键排序的前 limit 个记录:
Client.first(2)
# => [#<Client id: 1, first_name: "Lifo">, #<Client id: 2, first_name: "Raf">]
1.3 批量获取多个对象
find_each 和 find_in_batches 方法的目的是分批处理无法一次载入内存的巨量记录。
如果只想遍历几千个记录,更推荐使用常规的查询方法。
1.3.1 find_each
find_each 方法获取一批记录,然后分别把每个记录传入代码块。在下面的例子中,find_each 获取 1000 各记录,然后把每个记录传入代码块,知道所有记录都处理完为止:
User.find_each do |user|
NewsLetter.weekly_deliver(user)
end:batch_size:batch_size 选项指定在把各记录传入代码块之前,各批次获取的记录数量。例如,一个批次获取 5000 个记录:User.find_each(batch_size: 5000) do |user|
NewsLetter.weekly_deliver(user)
end:startUser.find_each(start: 2000, batch_size: 5000) do |user|
NewsLetter.weekly_deliver(user)
end1.3.2 find_in_batches
find_in_batches 方法和 find_each 类似,都获取一批记录。
二者的不同点是,find_in_batches 把整批记录作为一个数组传入代码块,而不是单独传入各记录。
2 条件查询
where 方法用来指定限制获取记录的条件,用于 SQL 语句的 WHERE 子句。条件可使用字符串、数组或 Hash 指定。
2.1 纯字符串条件
如果查询时要使用条件,可以直接指定。例如 Client.where(:orders_count => 2),获取 orders_count 字段为 2 的客户记录。
2.2 数组条件
如果数字是在别处动态生成的话应该怎么处理呢?可用下面的查询:
Client.where("orders_count = ?", params[:orders])
Active Record 会先处理第一个元素中的条件,然后使用后续元素替换第一个元素中的问号(?)。
2.4 NOT 条件
SQL NOT 查询可用 where.not 方法构建。
Post.where.not(author: author)
也即是说,这个查询首先调用没有参数的 where 方法,然后再调用 not 方法。
3 排序
要想按照特定的顺序从数据库中获取记录,可以使用 order 方法。
例如,想按照 created_at 的升序方式获取一些记录,可以这么做:
Client.order(:created_at)
还可使用 ASC 或 DESC 指定排序方式:
Client.order(created_at: :desc)
Client.order(created_at: :asc)
Client.order(orders_count: :asc, created_at: :desc)
Client.order(:orders_count, created_at: :desc)
order,可以在前一个 order 后再调用一次:Client.order("orders_count ASC").order("created_at DESC")
4 查询指定字段
默认情况下,Model.find 使用 SELECT * 查询所有字段。要查询部分字段,可使用 select 方法。
Client.select("viewable_by")
查询出来的结果为只显示viewable_by字段内容
如果查询时希望指定字段的同值记录只出现一次,可以使用 distinct 方法:
Client.select(:name).distinct
查询后还可以删除唯一性限制:
query = Client.select(:name).distinct
query.distinct(false)
5 限量和偏移
limit 方法指定获取的记录数量,offset 方法指定在返回结果之前跳过多少个记录。例如:
Client.limit(5).offset(30)
这时会从第 31 个记录开始,返回最多 5 个客户对象。
6 分组
要想在查询时使用 SQL GROUP BY 子句,可以使用 group 方法。
7 分组筛选
SQL 使用 HAVING 子句指定 GROUP BY 分组的条件。在 Model.find 方法中可使用 :having 选项指定 HAVING 子句。

4418

被折叠的 条评论
为什么被折叠?



