@huobazi 和 Closure 发音一样:http://dict.youdao.com/search?q=closure
如果考虑基于 CloudFoundry 的 PASS 的话,可以看一下 http://www.mopaas.com/#price_plan
得到 price > 0
的计数:
Animal.select('max(id) as id, name, sum(number) as number').where('price > 0').group(:name)
# => [#<Animal id: 3, name: "乌龟", number: 2>, #<Animal id: 6, name: "猫", number: 4>]
如果要得到所有的计数(不限制 price),应该需要分开做:
animals = Animal.select('max(id) as id, name').where('price > 0').group(:name)
# => [#<Animal id: 3, name: "乌龟">, #<Animal id: 6, name: "猫">]
numbers = Animal.group(:name).sum(:number)
# => {"乌龟"=>3, "猫"=>4}
animals.each { |animal| animal.number = numbers[animal.name] }
# => [#<Animal id: 3, name: "乌龟", number: 3>, #<Animal id: 6, name: "猫", number: 4>]
方法一(会执行两条 SQL 语句):
Animal.where(id: Animal.select('max(id) as id').where('price > 0').group(:name).pluck(:id))
方法二(一条 SQL):
Animal.find_by_sql('select b.* from (select max(id) as id from animals where price > 0 group by name) a inner join animals b on (a.id = b.id)')
设置 set :deploy_via, :remote_cache 就可以了,可以参考 http://www.iteye.com/topic/1114390
推荐 ember.js