find_all on an array of ActiveRecords

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:

@entries = @account.entries.find_all{|entry|false}

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!)

Can anybody point out where I am wrong?

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:

@entries = @account.entries.select{|entry|entry.tags.collect{|tag| tag.name}.include?(@tag.name)}

this would also work: @entries = @account.entries.to_a.find_all{|entry|entry.tags.collect{| tag> tag.name}.include?(@tag.name)}

or, use AR for the whole operation: @entries = @account.entries.find(:all, :include => :tags, :conditions => ["tags.name = ?",@tag.name]) (untested)

Less code, less records fetched by SQL ...

Thank you both, this is very helpful