文章摘要
1、获取数据
获取第一条、最后一条记录
Model.first
Model.first(options)
Model.find(:first, options)
Model.last
Model.last(options)
Model.find(:last, options)通过id获取记录
Model.find(1, 10, options)
Model.find([1, 10], options)
.find all对一组数据进行相同操作
User.all.each do |user|
NewsLetter.weekly_deliver(user)
endUser.find_each do |user|
NewsLetter.weekly_deliver(user)
endUser.find_each(:batch_size=> 5000, :start=> 2000) do |user|
NewsLetter.weekly_deliver(user)
endInvoice.find_in_batches(:include=> :invoice_lines) do |invoices|
export.add_invoices(invoices)
end2、查询条件
通过替换?来传递条件值,可避免SQL注入
如果要生成日期时间,再加上.to_time
有上数据库会在以上条件中报错,如Mysql会报查询语句过长的错误,此时可以改成created_at > ? AND created_at < ?的形式
Hash条件
3、查询选项
排序
#单个排序
Client.all(:order=> “created_at ASC”)
#多个排序
Client.all(:order=> “orders_count ASC, created_at DESC”)Client.all(:select=> “viewable_by, locked”)
#使用函数
Client.all(:select=> “DISTINCT(name)”)Client.all(:limit=> 5)
#生成
SELECT * FROM clients LIMIT 5
Client.all(:limit=> 5, :offset=> 5)
#生成
SELECT * FROM clients LIMIT 5, 5生成sql
Having
生成sql
client=Client.first(:readonly=> true)
client.locked=false
client.save
#对只读对象进行保存将会触发ActiveRecord::ReadOnlyRecord异常更新时锁定记录
乐观锁Optimistic Locking
为使用乐观锁,须在表里建一个lock_version的字段,每次更新记录时,ActiveRecord自动递增lock_version的值,This behavior can be turned off by setting ActiveRecord::Base.lock_optimistically=false.
指定乐观锁字段名
悲观锁Pessimistic Locking
悲观锁定由数据库直接提供Item.transaction do
i=Item.first(:lock=> true)
i.name=’Jones’
i.save
endMysql执行返回
为特定数据库加入原始的lock声明
为Mysql的锁定声明为共享模式,即锁定时仍然可读
4、关联表
生成sql
使用Array、Hash、Named Associations关联表
有如下modelclass Category < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :category
has_many :comments
has_many :tags
end
class Comments
belongs_to :post
has_one :guest
end
class Guest < ActiveRecord::Base
belongs_to :comment
end#关联一个关系
Category.all :joins=> :posts
#关联多个关系
Post.all :joins=> [:category, :comments]
#嵌套关联
Category.all :joins=> {:posts=> [{:comments=> :guest}, :tags]}time_range=(Time.now.midnight – 1.day)..Time.now.midnight Client.all :joins=> :orders, :conditions=> {‘orders.created_at’=> time_ran
#或者
time_range=(Time.now.midnight – 1.day)..Time.now.midnight Client.all :joins=> :orders, :conditions=> {:orders=> {:created_at=> time_range}}5、优化载入
以下代码,需要执行1 + 10次sqlclients=Client.all(:limit=> 10) clients.each do |client|
puts client.address.postcode
endclients=Client.all(:include=> :address, :limit=> 10)
clients.each do |client|
puts client.address.postcode
endClient.find_by_name(“Ryan”)
Client.find_all_by_name(“Ryan”)
#!方法,没有记录时抛出ActiveRecord::RecordNotFound异常
Client.find_by_name!(“Ryan”)
#查询多个字段
Client.find_by_name_and_locked(“Ryan”, true)
#查询不到时就创建并保存
Client.find_or_create_by_name(params[:name])
#查询不到时创建一个实例,但不保存
Client.find_or_initialize_by_name(‘Ryan’)8、select_all
和find_by_sql类似,但不会用model实例化返回记录,你会得到一个hash数组#通过id来查询
Client.exists?(1)
Client.exists?(1, 2, 3)
#or
Client.exists?([1,2,3])
#通过其他条件来查询
Client.exists?(:conditions=> “first_name=’Ryan'”)
#没有参数时,则:表是空的 ? false : true
Client.exists?#求结果集条数
Client.count(:conditons=> “first_name=’Ryan'”)
#求某个字段非空白的条数
Client.count(:age)
#平均值
Client.average(“orders_count”)
#求最小值
Client.minimum(“age”)
#求最大值
Client.maximum(“age”)
#求和
Client.sum(“orders_count”)