Hola gente,
Llevo un rato dándole vueltas a este asunto y no logro encontrar donde
está el error/mal entendimiento/mal uso/bug/feature…
Resulta que extraigo todos los elementos de una relación has_many y le
quiero pasar un sum para sumar uno de los campos de los objetos del
array tal que
así:
Tengo el modelo Cart con estos has_many
has_many :carts_events, :dependent => :destroy
has_many :events, :through => :carts_events
Y el modelo Evento que tiene un atributo price_cents:integer
Fijaros en el through pero como veremos más abajo ocurre de igual modo
si uso el array del carts_events que no tiene through
Invoco al sum tal que
así: cart.events.sum{ |e| e.price_events }
Y me suelta este error:
ArgumentError: wrong number of arguments (1 for 2)
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:368:in
calculate' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:368:in
send’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:368:in
method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2003:in
with_scope’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in
send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in
with_scope’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:366:in
method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:151:in
sum’
from (irb):19
Ahora un montón de comprobaciones:
Event.all.sum{ |e| e.price_cents }
=> 300
BIEN!
cart.events.class
=> ArrayEvent.all.class
=> Array
BIEN!
cart.events
=> [#<Event id: 491222053, name: “Event 1”, permalink: “event-1”,
description: “Just an Event”, price_cents: 100, created_at:
“2009-01-22 00:47:38”, updated_at: “2009-01-22 00:47:38”>, #<Event id:
491222054, name: “Event 2”, permalink: “event-2”, description: “Just
another Event”, price_cents: 200, created_at: “2009-01-22 00:47:38”,
updated_at: “2009-01-22 00:47:38”>]Event.all
=> [#<Event id: 491222053, name: “Event 1”, permalink: “event-1”,
description: “Just an Event”, price_cents: 100, created_at:
“2009-01-22 00:47:38”, updated_at: “2009-01-22 00:47:38”>, #<Event id:
491222054, name: “Event 2”, permalink: “event-2”, description: “Just
another Event”, price_cents: 200, created_at: “2009-01-22 00:47:38”,
updated_at: “2009-01-22 00:47:38”>]
BIEN!
Event.all == cart.events
=> true
BIEN!
?> cart.events.ancestors
=> [Event(id: integer, name: string, permalink: string, description:
text, price_cents: integer, created_at: datetime, updated_at:
datetime), ActiveRecord::Base, ActiveRecord::Serialization,
ActiveRecord::Calculations, ActiveRecord::Reflection,
ActiveRecord::Transactions, ActiveRecord::Aggregations,
ActiveRecord::AssociationPreload, ActiveRecord::NamedScope,
ActiveRecord::Associations, ActiveRecord::Timestamp,
ActiveRecord::Observing, ActiveRecord::Callbacks, ActiveRecord::Dirty,
ActiveRecord::AttributeMethods, ActiveRecord::Locking::Pessimistic,
ActiveRecord::Locking::Optimistic, ActiveSupport::Callbacks,
ActiveRecord::Validations, Object,
ActiveSupport::Dependencies::Loadable, InstanceExecMethods,
Base64::Deprecated, Base64, Kernel]
Event.all.ancestors
NoMethodError: undefined method `ancestors’ for #Array:0x21da568
from (irb):28
OUCH!.. digo FAIL!
Para ver que el through no está causando el error:
cart.carts_events.sum{ |ce| ce.event.price_event }
ArgumentError: wrong number of arguments (1 for 2)
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:368:in
calculate' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:368:in
send’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:368:in
method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2003:in
with_scope’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in
send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in
with_scope’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:366:in
method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:151:in
sum’
from (irb):37
FAIL!
Pero es que además:
cart.events.sum
ArgumentError: wrong number of arguments (1 for 2)
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:370:in
calculate' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:370:in
send’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:370:in
method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2003:in
with_scope’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in
send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in
with_scope’
from
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:366:in
method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:153:in
sum’
from (irb):38
No entiendo… recibe un argumento y esperaba 2 :?
En la línea donde peta pone esto:
@reflection.klass.send(method, *args)
No he querido meterme ahí a debuggear por saber si esque la
soluciónla tengo delante y no la veo.
Qué me decís?
Saludos
f.