ActiveRecord::Calculations模块提供了model级的方法来得到model数据的count,sum,average,maximum和minimum值
例如:
[code]
Person.count("age > 21")
Person.average(:age)
Person.minimum(:age)
Person.maximum(:age)
Person.average(:age, :conditions => ["name like ?", '%T%'])
Donation.sum(:amount, :group => :created_at)
Donation.sum(:amount, :group => 'YEAR(created_at)')
Rating.average(:value, :group => :post).collect{|post, rating| [post.title, rating]}
Rating.average(:value, :group => :post)[Post.find(2)]
Person.find_by_name("Adam").ratings.average(:value)
[/code]
例如:
[code]
Person.count("age > 21")
Person.average(:age)
Person.minimum(:age)
Person.maximum(:age)
Person.average(:age, :conditions => ["name like ?", '%T%'])
Donation.sum(:amount, :group => :created_at)
Donation.sum(:amount, :group => 'YEAR(created_at)')
Rating.average(:value, :group => :post).collect{|post, rating| [post.title, rating]}
Rating.average(:value, :group => :post)[Post.find(2)]
Person.find_by_name("Adam").ratings.average(:value)
[/code]
本文介绍如何使用 ActiveRecord::Calculations 模块提供的方法,包括 count、sum、average、maximum 和 minimum,来高效地获取数据库中特定字段的统计数据。
2185

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



