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 データベースを使用しています。ルビーを使用して結果を合計することに頼りたくありません。また、このタイプの単純な集計クエリに対して生の SQL を実行することに頼るのは、間違っていると感じています。
誰でもこれに光を当てることができますか?DataMapper のチェーン ファインダー、特に集計に関して、正しい方向性を示していただければ幸いです。API と DataMapper サイトを調べてみましたが、まだ解決策はありません。