In my project, an account has_many entries and an entry
has_and_belongs_to_many tags.
I want to find all of the entries for an account which are tagged with
"some specific tag"
The code which I thought would do this is:
@entries = @account.entries.find_all{|entry|entry.tags.collect{|tag|
tag.name}.include?(@tag.name)}
However, this returns an array containing all entries for the account,
and ignores the include? condition.
While attempting to debug the problem, I simplified the expression:
This too returns an array containing all entries for the account.
@account.entries.class is an Array, so I assume the find_all which I
am calling is Enum#find_all and not ActiveRecord#find_all (whic have
much different functionality!)
I think it indeed IS the ActiveRecord#find_all Method, for the
following reason:
if you call the find_all method directly on the association like:
@account.entries.find_all
without the entries being eager-loaded before, it does an AR#find_all
because @account.entries doesn't hold any objects yet.
This would also explain why the block isn't evaluated: AR#find_all
doesn't take a block.
Rails aliases Enumerable#find with #detect, and #find_all with
#select: