Hi. In this setup "account has_many :consultations". Can anyone explain the following behaviour to me? Rails 1.2.2. The "sum" method should return 0 for an empty array.>> a = Account.find(1)=> #<Account:0x39c80a8 @attributes={...}>> a.consultations=> []>> a.consultations.sum(&:foo)ArgumentError: wrong number of arguments (1 for 2) from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/associations/has_many_through_association.rb:110:in `calculate'' from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/associations/has_many_through_association.rb:110:in `send'' from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/associations/has_many_through_association.rb:110:in `method_missing'' from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/base.rb:946:in `with_scope'' from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/associations/has_many_through_association.rb:110:in `method_missing'' from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/associations/has_many_through_association.rb:102:in `sum'' from (irb):15 But if I use a hard coded empty array, things work:>> [].sum(&:foo)=> 0 What''s happening? Thanks. Morten --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey Morten, yeah, I feel your pain sharply, because it happened to me too. It turns out that associations favor the ActiveRecord sum method (which issues an sql query) over the Enumerable sum method (which iterates over the collection). Here''s something that''s not fully tested by me but you can stick it in your environment.rb and see how it works for you. Once I test it better I''ll submit it as a patch. http://pastie.caboo.se/48812 When you don''t pass a block to sum() it''ll end up delegating to the AR calculate() method, otherwise it iterates. so: collection.sum :id # => issues an sql query via calculate() collection.sum &:id => iterates over the collection due to :id.to_proc HTH Trevor On 22-Mar-07, at 5:48 AM, Morten wrote:> > > Hi. > > In this setup "account has_many :consultations". Can anyone explain > the following behaviour to me? Rails 1.2.2. The "sum" method should > return 0 for an empty array. > >>> a = Account.find(1) > => #<Account:0x39c80a8 @attributes={...} >>> a.consultations > => [] >>> a.consultations.sum(&:foo) > ArgumentError: wrong number of arguments (1 for 2) > from ./script/../config/../config/../vendor/rails/ > activerecord/ > lib/active_record/associations/has_many_through_association.rb:110:in > `calculate'' > from ./script/../config/../config/../vendor/rails/ > activerecord/ > lib/active_record/associations/has_many_through_association.rb:110:in > `send'' > from ./script/../config/../config/../vendor/rails/ > activerecord/ > lib/active_record/associations/has_many_through_association.rb:110:in > `method_missing'' > from ./script/../config/../config/../vendor/rails/ > activerecord/ > lib/active_record/base.rb:946:in `with_scope'' > from ./script/../config/../config/../vendor/rails/ > activerecord/ > lib/active_record/associations/has_many_through_association.rb:110:in > `method_missing'' > from ./script/../config/../config/../vendor/rails/ > activerecord/ > lib/active_record/associations/has_many_through_association.rb:102:in > `sum'' > from (irb):15 > > But if I use a hard coded empty array, things work: > >>> [].sum(&:foo) > => 0 > > What''s happening? Thanks. > > Morten > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Trevor. I also got informed that an explicit .to_a will work (which it did for me): collection.to_a.sum(&:id) Br, Morten On 22 Mar., 18:39, Trevor Squires <tre...-k8q5a0yEZAgS+FvcfC7Uqw@public.gmane.org> wrote:> Hey Morten, > > yeah, I feel your pain sharply, because it happened to me too. > > It turns out that associations favor the ActiveRecord sum method > (which issues an sql query) over the Enumerable sum method (which > iterates over the collection). > > Here''s something that''s not fully tested by me but you can stick it > in your environment.rb and see how it works for you. Once I test it > better I''ll submit it as a patch. > > http://pastie.caboo.se/48812 > > When you don''t pass a block to sum() it''ll end up delegating to the > AR calculate() method, otherwise it iterates. > > so: > > collection.sum :id # => issues an sql query via calculate() > collection.sum &:id => iterates over the collection due to :id.to_proc > > HTH > Trevor > > On 22-Mar-07, at 5:48 AM, Morten wrote: > > > > > Hi. > > > In this setup "account has_many :consultations". Can anyone explain > > the following behaviour to me? Rails 1.2.2. The "sum" method should > > return 0 for an empty array. > > >>> a = Account.find(1) > > => #<Account:0x39c80a8 @attributes={...} > >>> a.consultations > > => [] > >>> a.consultations.sum(&:foo) > > ArgumentError: wrong number of arguments (1 for 2) > > from ./script/../config/../config/../vendor/rails/ > > activerecord/ > > lib/active_record/associations/has_many_through_association.rb:110:in > > `calculate'' > > from ./script/../config/../config/../vendor/rails/ > > activerecord/ > > lib/active_record/associations/has_many_through_association.rb:110:in > > `send'' > > from ./script/../config/../config/../vendor/rails/ > > activerecord/ > > lib/active_record/associations/has_many_through_association.rb:110:in > > `method_missing'' > > from ./script/../config/../config/../vendor/rails/ > > activerecord/ > > lib/active_record/base.rb:946:in `with_scope'' > > from ./script/../config/../config/../vendor/rails/ > > activerecord/ > > lib/active_record/associations/has_many_through_association.rb:110:in > > `method_missing'' > > from ./script/../config/../config/../vendor/rails/ > > activerecord/ > > lib/active_record/associations/has_many_through_association.rb:102:in > > `sum'' > > from (irb):15 > > > But if I use a hard coded empty array, things work: > > >>> [].sum(&:foo) > > => 0 > > > What''s happening? Thanks. > > > Morten--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---