>> [].eql? @product.files=> false>> @product.files.eql? []=> true version: 2.3.5 Product :has_many files -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 14, 2:06 pm, Alexander <cutal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> >> [].eql? @product.files > => false > >> @product.files.eql? [] >You''ve uncovered Active Record''s terrible secret, files isn''t actually an Array. Fred> => true > > version: 2.3.5 > Product :has_many files-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > You''ve uncovered Active Record''s terrible secret, files isn''t actually > an Array. >Actually I''d call it magic or voodoo, but not a dirty secret. It''s actually a brilliant/necessary part of the arel features in Rails 3. When you select a recordset using Rails 3 arel features (where, order, etc), it doesn''t actually call the DB until you iterate over the resultset. This means if that part of the view is cached, it won''t actually hit your DB at all. If files was an array it would have had to call the DB to fill the array, only for it not to be used. So, the OP''s code snippet is not surprising - @product.files is not an array, but if you use the eql? method on that object it will compare it''s internal data to an empty array (but arrays know nothing about this ActiveRecord class). Cheers, Andy -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yes, I know about this AR behavior. I think, would be correct if the comparison works both ways. For beginners, it looks like a bug. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alexander G. wrote:> Yes, I know about this AR behavior. > I think, would be correct if the comparison works both ways. > For beginners, it looks like a bug.Is it really worth monkeypatching Array#eql? for this? I think not. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > > Yes, I know about this AR behavior. > > I think, would be correct if the comparison works both ways. > > For beginners, it looks like a bug. > > Is it really worth monkeypatching Array#eql? for this? I think not. >I almost posted the same thing earlier, but had a second thought. Would it not be easier to create a to_a method on the ActiveRecord resultset object (whatever that is)? It''s not going to be as efficient as it will then (at that point) need to instantiate all result objects to compare, but it would then work both ways round... But I didn''t get a chance to look further in to it to test the theory/idea. Cheers, Andy -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.