I was unable to reproduce this problem using objects from one of my applications – Country which has_many :places. But I did notice that when I ran country1.places.inspect in the Rails console, the output was not surrounded by square brackets, whereas yours is.
By chance, have you explicitly defined a method named things?
Yes, things is a model method, which returns a CollectionProxy containing other.things.
def things
self.other.joins(:things).map(&:things)
end
I’m not actually using this code or anything like it; my needs were better served by a scope and a method on the things model. I’m just curious why .include? will not see the object in the CollectionProxy when it’s obviously there. Could be useful to know for the future under what circumstances .include? cannot be used.
So in the statement object.things.include?(other.thing), the Array#include? method is comparing CollectionProxy objects to a Thing object, therefore the comparison always fails.
Assuming that the class definition for object and other includes the statement has_many :things, Rails will automatically provide a #things method. You’re overriding (monkey-patching) that method, which is not a good practice.
The method provided by Rails would return a single CollectionProxy object rather than an Array of them, and the CollectionProxy#include? method would return true.