Array.wrap handling of association collections

Hi all,

Good to see Array.wrap get added. However it doesn't DWIM with associations:

Array.wrap(Icp.find(16).registers)

=> [[#<Register ...>]]

Array.wrap(DisputedRead.unresolved)

=> [[#<DisputedRead ...>, #<DisputedRead ...>]]

ie. array-like collections and scopes aren't getting treated as arrays and are being wrapped again.

The short-term solution the coder who hit this suggested was to add a case to the #wrap:

          when ActiveRecord::NamedScope::Scope, ActiveRecord::Associations::AssociationCollection             object.to_a

But this has the effect of loading ActiveRecord from ActiveSupport, which is undesirable. Is there a better way to detect association-collection-ness?

I'm thinking that it might be better to check for the #to_ary method on all objects and use that instead, without having a special case for collections/scopes. Does that sound OK? If so I'll submit a patch.

Cheers, Will

Yeah, good call. Please do!

jeremy

I'm thinking that it might be better to check for the #to_ary method on all objects and use that instead, without having a special case for collections/scopes. Does that sound OK? If so I'll submit a patch.

Yeah, good call. Please do!

Thanks, have filed #1935 Array.wrap handling of association collections - Ruby on Rails - rails.

Cheers, Will