Note that there are some explanatory texts on larger screens.

plurals
  1. POChained aggregate call across association in DataMapper (ruby)
    text
    copied!<p>I am working on a simple budget app using Sinatra and DataMapper in Ruby.</p> <p>I want to get the sum of all transactions across all income accounts within the last 30 days.</p> <p>Something like <code>Account.income_accounts.account_entries.sum(:amount, :transaction_date.gte =&gt; Date.today - 30)</code> should work. Instead, the limiting condition on <code>transaction_date</code> is getting ignored, returning the sum of the amount for all entries for all income accounts.</p> <p>Given the following:</p> <pre><code>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 =&gt; '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 </code></pre> <p>I am properly requiring <code>dm-aggregates</code>. I am new to DataMapper. If it matters, I am using a sqlite3 database. I really don't want to resort to using ruby to sum the results. It also feels wrong to resort to executing raw SQL for this type of simple aggregate query. </p> <p>Can anyone shed some light on this? I would love to be pointed in the right direction regarding chained finders in DataMapper, particularly with aggregates. My spelunking into the API and the DataMapper site hasn't yielded a solution as of yet.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload