ActiveRecord“has_aggregate”元编程功能?

时间:2012-01-16 19:20:50

标签: ruby-on-rails activerecord

那里有什么提供“has_aggregate”元编程功能吗?

例如,假设您有一个提供以下方法的模型:

class Customer < ActiveRecord::Base

  has_many :orders

  def total_orders
    orders.count
  end

  def total_spent
    orders.sum(:total_amount)
  end

end

ActiveRecord(或gem)中是否有任何允许语法类似的功能?

class Customer < ActiveRecord::Base

  has_many :orders

  has_aggregate :total_orders, :orders, :count

  has_aggregate :total_spent, :orders, :sum, :total_amount

end

0 个答案:

没有答案