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.