我正在使用 Ruby 中的 Sinatra 和 DataMapper 开发一个简单的预算应用程序。
我想获得过去 30 天内所有收入账户中所有交易的总和。
类似的东西Account.income_accounts.account_entries.sum(:amount, :transaction_date.gte => Date.today - 30)
应该可以工作。相反,限制条件 ontransaction_date
被忽略,返回所有收入账户的所有条目的金额总和。
鉴于以下情况:
class Account
include DataMapper::Resource
has n, :account_entries
property :id, Serial
property :name, String
property :acct_type, String
def self.income_accounts
all(:acct_type => 'Income')
end
end
class AccountEntry
include DataMapper::Resource
belongs_to :account
property :id, Serial
property :account_id, Integer
property :description, String
property :amount, BigDecimal
property :transaction_date, DateTime
end
我正确地要求dm-aggregates
. 我是 DataMapper 的新手。如果重要的话,我使用的是 sqlite3 数据库。我真的不想诉诸使用 ruby 来总结结果。对这种类型的简单聚合查询执行原始 SQL 也是错误的。
任何人都可以对此有所了解吗?我很想指出关于 DataMapper 中的链式查找器的正确方向,尤其是聚合。我对 API 和 DataMapper 站点的深入研究尚未产生解决方案。