What I want to do is something like this: searchid = 4 while searchid != -1 @a += A.find_by_someid(searchid) @b = B.find_by_someid(searchid) searchid = @b.parentid end The problem being the line @a += A.find_by_someid(searchid) The error being something like NoMethodError: undefined method `+'' for #<A:0x173f9a0> I tried to simply do a @a=[] but then it complains ''A'' cannot be converted into an Array. So, how do you combine multiple ''find'' requests? And what format do the finds return if not an array? The output from a find looks something like: => #<A id: 99, name: "A Name"> Thanks -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
The .find_by_....(...) meth returns the first model object found (or nil if none found), whereas .find(:all, ...) meth returns an array: $ ./script/console>> puts Foo.find_by_id(1).classFoo>> puts Foo.find_by_id(0).classNilClass>> puts Foo.find(:all).classArray Jeff On May 14, 12:09 pm, Hubert Yoshizaki <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> What I want to do is something like this: > > searchid = 4 > while searchid != -1 > @a += A.find_by_someid(searchid) > @b = B.find_by_someid(searchid) > searchid = @b.parentid > end > > The problem being the line > > @a += A.find_by_someid(searchid) > The error being something like > > NoMethodError: undefined method `+'' for #<A:0x173f9a0> > > I tried to simply do a @a=[] but then it complains ''A'' cannot be > converted into an Array. > > So, how do you combine multiple ''find'' requests? > And what format do the finds return if not an array? > The output from a find looks something like: > => #<A id: 99, name: "A Name"> > > Thanks > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On May 14, 2:09 pm, Hubert Yoshizaki <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> What I want to do is something like this: > > searchid = 4 > while searchid != -1 > @a += A.find_by_someid(searchid) > @b = B.find_by_someid(searchid) > searchid = @b.parentid > end > > The problem being the line > > @a += A.find_by_someid(searchid) > The error being something like > > NoMethodError: undefined method `+'' for #<A:0x173f9a0> > > I tried to simply do a @a=[] but then it complains ''A'' cannot be > converted into an Array. > > So, how do you combine multiple ''find'' requests? > And what format do the finds return if not an array? > The output from a find looks something like: > => #<A id: 99, name: "A Name">I''m not exactly sure I understand what you''re trying to do, but I think you want something like this: @a = [] # start with an empty array @a << A.find_by_id(some_id) # returns an object, or null if not found; push into array @a << B.find_by_id(some_id) # returns an object, or null if not found; push into array @a.compact! # removes null entries from the array Does that help? Jeff www.purpleworkshops.com> Thanks > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ahh that works, thanks! I think I had the general idea but I think every time I tried @a = [] I tried @a += A.find_by_id(some_id) while every time I tried @a << A.find_by_id(some_id) I never tried the @a = [] ....whoops, thanks for all the help. Plus never seen @a.compact!, helps a lot :) btw, in general what I was looking for was a way to put the .find methods inside a loop and combine all the results into one result, everything else was just fluff. Jeff Cohen wrote:> On May 14, 2:09�pm, Hubert Yoshizaki <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> The output from a find looks something like: >> => #<A id: 99, name: "A Name"> > > I''m not exactly sure I understand what you''re trying to do, but I > think you want something like this: > > @a = [] # start with an empty array > > @a << A.find_by_id(some_id) # returns an object, or null if not > found; push into array > @a << B.find_by_id(some_id) # returns an object, or null if not > found; push into array > > @a.compact! # removes null entries from the array > > Does that help? > > Jeff > > www.purpleworkshops.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ahh, thanks. Should of thought of that one. Quick question, when you do a .find(:all) I''m assuming it returns an array of objects? Jeff Burlysystems wrote:> The .find_by_....(...) meth returns the first model object found (or > nil if none found), whereas .find(:all, ...) meth returns an array: > > $ ./script/console > >>> puts Foo.find_by_id(1).class > Foo > >>> puts Foo.find_by_id(0).class > NilClass > >>> puts Foo.find(:all).class > Array > > Jeff-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
From the rails api docs examples (http://api.rubyonrails.org/classes/ ActiveRecord/Base.html#M002263): ... # find all Person.find(:all) # returns an array of objects for all the rows fetched by SELECT * FROM people Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50) Person.find(:all, :conditions => { :friends => ["Bob", "Steve", "Fred"] } Person.find(:all, :offset => 10, :limit => 10) Person.find(:all, :include => [ :account, :friends ]) Person.find(:all, :group => "category") ... Jeff On May 14, 2:24 pm, Hubert Yoshizaki <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Ahh, thanks. Should of thought of that one. Quick question, when you do > a .find(:all) I''m assuming it returns an array of objects? > > > > > > Jeff Burlysystems wrote: > > The .find_by_....(...) meth returns the first model object found (or > > nil if none found), whereas .find(:all, ...) meth returns an array: > > > $ ./script/console > > >>> puts Foo.find_by_id(1).class > > Foo > > >>> puts Foo.find_by_id(0).class > > NilClass > > >>> puts Foo.find(:all).class > > Array > > > Jeff > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, May 14, 2010 at 2:24 PM, Hubert Yoshizaki <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Quick question, when you do > a .find(:all) I''m assuming it returns an array of objects?No need to assume :-)>> foos = Foo.all......>> foos.class=> Array>>FWIW, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ah, makes perfect sense. Thanks. :) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.