那里有什么提供“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