Hi Sergio,

All the Joins are created automatically by AS because of the various belongs_to and has_many structures in ActiveRecord, and not by something that I'm including in AS.

I was therefore wondering if the code that computes the Sum, Avg etc in AS is right. It seems to be that it is causing too many elements to be included in the database queries which is causing the result to be wrong.
I exported all the numbers to Excel and added them up. The Rails console is right.

So would a solution be to use code similar to the rails code below inside of AS. In my opinion we either need to remove the AS ability to do math on the columns or get it to compute right. Having random numbers being added to the sum is counter productive.

/S



-------- Original Message --------
Subject: Re: SUM in AS is reporting the WRONG number.
From: "Sergio Cambra .:: entreCables S.L. ::." <[email protected]>
Date: Wed, September 08, 2010 1:02 am
To: [email protected]

On Miércoles, 8 de Septiembre de 2010 03:56:04 Soren escribió:
> Hi,
>
> I have an embedded scaffold that done like this:
>
> <%= render :active_scaffold => "Donations",
>
> :constraints => { :campaign_id => @campaign.id } %>
>
> the amount column has a sum function attached to it. The resulting SQL
> is then:
>
> SELECT sum("donations".amount) AS sum_amount FROM "donations" LEFT
> OUTER JOIN "installments" ON installments.donation_id = donations.id
> LEFT OUTER JOIN "matching_grants" ON matching_grants.donation_id =
> donations.id LEFT OUTER JOIN "payments" ON payments.donation_id =
> donations.id LEFT OUTER JOIN "users" ON "users".id =
> "donations".user_id LEFT OUTER JOIN "students" ON "students".id =
> "donations".student_id WHERE ((("donations"."campaign_id" = 1)))
>
> and the associated result is
> 303205.24
>
> The same calculated in the Rails console:
> >> Donation.find_all_by_campaign_id(1).sum { |p| p.amount }
>
> => #<BigDecimal:107deefd8,'0.29340524E6',12(24)>
>
> So they differ - WHY ?
>
> Turns out that the inclusion of all the JOIN tables causes the number
> to go up.
> Running the same SQL query without the JOIN statements causes it to be
> less.
>
> In my tables there are other columns named amount. The sum should have
> been much bigger if they are in play.
>
> Can somebody explain what is happening here - and how to we get AS to
> report the right sums.

If you are joining some has_many associations, one amount can be summed
multiple times. If we remove joins, it will break if you define
conditions_for_collection method to add conditions using fields from joined
tables. You can set includes nil in each association column, you will disable
eager loading in list and you won't be able to add conditions using that
tables, but sum should work.

--
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) [email protected]

--
You received this message because you are subscribed to the Google Groups "ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/activescaffold?hl=en.

--
You received this message because you are subscribed to the Google Groups "ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/activescaffold?hl=en.

Reply via email to