1

我正在使用 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 站点的深入研究尚未产生解决方案。

4

4 回答 4

2

我刚刚编写了一个小的独立脚本来测试您的示例,它似乎返回了正确的结果。请注意,我使用的是从 git 安装的 edge extlib、dm-core 和 dm-more:

#!/usr/bin/env ruby -Ku

# encoding: utf-8

require 'rubygems'
require 'dm-core'
require 'dm-aggregates'

DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')

class Account
  include DataMapper::Resource

  property :id,        Serial
  property :name,      String
  property :acct_type, String

  has n, :account_entries

  def self.income_accounts
    all(:acct_type => 'Income')
  end
end

class AccountEntry
  include DataMapper::Resource

  property :id,               Serial
  property :description,      String
  property :amount,           BigDecimal
  property :transaction_date, Date

  belongs_to :account
end

DataMapper.auto_migrate!

account = Account.create(
  :name      => 'Test Account',
  :acct_type => 'Income'
)

5.times do |n|
  account.account_entries.create(
    :description      => "Account Entry #{n}",
    :amount           => 1.00,
    :transaction_date => Date.today
  )
end

puts Account.income_accounts.account_entries(:transaction_date.gte => Date.today - 30).sum(:amount).to_s('F')  # => 5.0

Can you run the above program and let me know what it returns for you? If you get something other than 5.0, try updating to the latest packages and retry.

于 2009-10-25T02:31:01.330 回答
0

DateTime 使用秒,因为它的基本单位Date.today - 30是 30前。尝试Date.today - 30.days

于 2009-10-24T19:47:51.140 回答
0

您是否尝试过 DateTime.now-30 甚至 Time.now-30*3600*24 而不是 Date.today-30 ?

于 2009-10-24T21:10:38.047 回答
0

用户错误。我胡乱使用to_sonDateTime中的时间格式strftime。移除后,链式聚合按预期工作。

于 2009-10-24T21:25:10.577 回答