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? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I think this should do it: @entries = @account.entries.find(:all, :include => :tags, :conditions => ["tags.name = ?", @tag.name]) Stijn -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 ... On 20 Jul., 12:44, Dave Roberts <dave.a.robe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you both, this is very helpful On Jul 20, 6:44 am, Dave Roberts <dave.a.robe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---