Douglas Shearer
2007-Aug-29 18:55 UTC
find( [1,2,3] ) find from array without raising error.
Hello! Best way to explain this is with some code.. id_arr = [1,2,3,4,5] # There is no record with id 5. => results = find(id_arr) ActiveRecord::RecordNotFound: Couldn''t find all Posts with IDs (1,2,3,4,5). From reading the documentation it appears that AR will produce this error if ALL the ids do not return errors, but it appears to error when one or more of the ids does not return. Any way to get those id''s which do exist to return, without the error? Cheers. Douglas F Shearer dougal.s-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://douglasfshearer.com -- 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 -~----------~----~----~----~------~----~------~--~---
Lionel Bouton
2007-Aug-29 19:04 UTC
Re: find( [1,2,3] ) find from array without raising error.
Douglas Shearer wrote:> Hello! > > Best way to explain this is with some code.. > > id_arr = [1,2,3,4,5] # There is no record with id 5. > => > results = find(id_arr) > ActiveRecord::RecordNotFound: Couldn''t find all Posts with IDs > (1,2,3,4,5). > > From reading the documentation it appears that AR will produce this > error if ALL the ids do not return errors, but it appears to error when > one or more of the ids does not return. > > Any way to get those id''s which do exist to return, without the error? >From memory (I don''t remember if you need something like id_arr.join('','') in the conditions instead of just id_arr): results = id_arr.empty? ? [] : find(:all, :conditions => [ ''id IN (?)'', id_arr ]) Lionel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bob Showalter
2007-Aug-29 19:09 UTC
Re: find( [1,2,3] ) find from array without raising error.
On 8/29/07, Douglas Shearer <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hello! > > Best way to explain this is with some code.. > > id_arr = [1,2,3,4,5] # There is no record with id 5. > => > results = find(id_arr) > ActiveRecord::RecordNotFound: Couldn''t find all Posts with IDs > (1,2,3,4,5). > > From reading the documentation it appears that AR will produce this > error if ALL the ids do not return errors, but it appears to error when > one or more of the ids does not return. > > Any way to get those id''s which do exist to return, without the error?There may be an easier way, but something like this will do it: results = id_arr.collect {|i| Model.find_by_id(i)}.compact or, more efficient, since it makes only one query: results = Model.find :all, :conditions => ["id in (#{id_arr.collect {''?''}.join('','')})", *id_arr] --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Douglas Shearer
2007-Aug-29 19:10 UTC
Re: find( [1,2,3] ) find from array without raising error.
Lionel Bouton wrote:> results = id_arr.empty? ? [] : find(:all, :conditions => [ ''id IN (?)'', > id_arr ])find(:all) seems to be the way to go, came up with the following just after I posted... find(:all, :conditions => ''id = '' + id_arr.inject{ |sum,i| sum.to_s + '' OR id = '' + i.to_s }) ... but yours is far cleaner, I think I shall go with that. Cheers. Douglas F Shearer dougal.s-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://douglasfshearer.com -- 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 -~----------~----~----~----~------~----~------~--~---
andrewbruce
2007-Aug-29 19:17 UTC
Re: find( [1,2,3] ) find from array without raising error.
Nicer to do: Thing.find(:all, :conditions => [''id IN (?)'', id_arr]) On Aug 29, 8:10 pm, Douglas Shearer <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Lionel Bouton wrote: > > results = id_arr.empty? ? [] : find(:all, :conditions => [ ''id IN (?)'', > > id_arr ]) > > find(:all) seems to be the way to go, came up with the following just > after I posted... > > find(:all, :conditions => ''id = '' + id_arr.inject{ |sum,i| sum.to_s + '' > OR id = '' + i.to_s }) > > ... but yours is far cleaner, I think I shall go with that. > > Cheers. > > Douglas F Shearer > douga...-Re5JQEeQqe80Tx58lXaADg@public.gmane.org://douglasfshearer.com > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Douglas Shearer
2007-Aug-29 19:22 UTC
Re: find( [1,2,3] ) find from array without raising error.
andrewbruce wrote:> Thing.find(:all, :conditions => [''id IN (?)'', id_arr])Great minds think alike! Nice to get confirmation that my (Slightly late relative to my original posting) idea is correct. Thanks guys! :o) Douglas F Shearer dougal.s-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://douglasfshearer.com -- 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 -~----------~----~----~----~------~----~------~--~---
Bob Showalter
2007-Aug-29 19:25 UTC
Re: find( [1,2,3] ) find from array without raising error.
On 8/29/07, andrewbruce <bruciemoose-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Nicer to do: > > Thing.find(:all, :conditions => [''id IN (?)'', id_arr])Hmm, I didn''t know that an array was automagically expanded in a parameter list like that. Handy, and much cleaner than my line noise :~) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Lionel Bouton
2007-Aug-29 20:00 UTC
Re: find( [1,2,3] ) find from array without raising error.
Bob Showalter wrote:> On 8/29/07, andrewbruce <bruciemoose-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Nicer to do: >> >> Thing.find(:all, :conditions => [''id IN (?)'', id_arr]) >> > > Hmm, I didn''t know that an array was automagically expanded in a > parameter list like that. Handy, and much cleaner than my line noise > :~) >But depending on the context you can have to test for id_arr.empty? ... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---