Hello to all who have replied to my question,

Thank you a lot for your help!

The issue is now sufficiently cleared up for me.
Althogh I'd like to do some more research into the topic,
I've got other things to get finished first.


Robert Dober wrote:
> Hmm I think it is better to give hints  instead of  saying where the 
> problem lies.
> Well I do not know where the problem lies, but what I meant was the
> following [...]
Well yes, in the beginning most people searched the problem
somewhere in the outside environment (even Matz did!).
But then it became evident that the problem was really inside
the method calls.


I think the most misleading information was this debug statement:
    p [:rks_class,  rks.class]
producing this output:
[:rks_class, Array]

And still, rks was not an Array!


As Pete later explained:

Pete Yandell wrote:
> 
> So vt is an ActiveRecord object, and vt.rks refers to an ActiveRecord
> association?
> 
> If this is the case, rks is not an Array as you claim; it's an
> ActiveRecord association object which is a proxy to the data.
> 

And Daniel had summed up the whole thing very clearly:

Daniel DeLorme wrote:
> Sven Suska wrote:
>>> [...] what's rks.class.ancestors?
>> [Array,
>>  ActiveSupport::CoreExtensions::Array::Conversions,
>>  Enumerable,
>>  Object,
>>  Kernel]
> 
> This is probably wrong. Try to do this instead:
>    (class << rks;self;end).ancestors
> and you'll probably find this:
>    [ActiveRecord::Associations::HasManyAssociation,
> ActiveRecord::Associations::AssociationCollection,
> ActiveRecord::Associations::AssociationProxy, Object, 
> Base64::Deprecated,
> Base64, Kernel]

Thank you for this idea! I was wondering if there was any means
to get behind this masquerade of the ActiveRecord proxy objects.

> 
> ActiveRecord associations forward most messages (including 'class') to 
> the
> underlying array structure, but some messages (like 'size') are part of 
> the
> association class itself.
> 
> I think what's happening is that by calling 'size' first, the objects 
> have not
> yet been fetched from the database so instead it uses a COUNT(*) query 
> to find
> the size. When calling 'length' (or any other method) first, the objects 
> are
> fetched from the database and placed in an array and the 'length' 
> message is
> forwarded to that array. Then, when 'size' is invoked again, it notices 
> that the
> array is already present and uses its size instead of wasting time doing 
> another
> DB query. Normally the results should be the same but in your case 
> something
> weird is happening. I don't know what exactly but this should give you 
> some
> avenues to investigate.
> 

Yes, the main thing is very clear now, thank you.
(As I said, my db-interface is unable to do a proper "count".)


Having gone through this "ordeal", I'm arriving at this question:
Is it a good idea that the ActiveRecord proxy objects return Array
as the class of the proxy?
Or, even more: is this somehow "forced" by other design considerations?

For example, if there were "protocols" in ruby, people were less
often tempted to write
  obj.is_a?(Array)
And this is probably the reason, why th AR designers have made
the proxy objects say their class is Array. (And they indeed behave
like Arrays -- provided the db-interface passes all the unit tests.)


Anyway, I don't have the time now to go into a new thread.


I wish you all the best

Sven




-- 
Posted via http://www.ruby-forum.com/.