John Merlino
2011-Jun-24 18:42 UTC
instantiating object and passing a new array into the block
Hey all, Out of curiousity, all in Rails grabs all the records and converts them into an array. Map then iterates through the returned array, returning a new array. So why even use map here? And then the array gets passed as local variable bt. And we instantiate object and assign the bt array as a value of a hash of book_type. I dont see why map is needed here. book_details = BookType.all.map do |bt| Student.new(:book_type => bt) end Thanks for response. -- 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.
Walter Lee Davis
2011-Jun-24 21:50 UTC
Re: instantiating object and passing a new array into the block
On Jun 24, 2011, at 2:42 PM, John Merlino wrote:> Hey all, > > Out of curiousity, all in Rails grabs all the records and converts > them into an array. Map then iterates through the returned array, > returning a new array. So why even use map here? And then the array > gets passed as local variable bt. And we instantiate object and assign > the bt array as a value of a hash of book_type. I dont see why map is > needed here. > > > book_details = BookType.all.map do |bt| > Student.new(:book_type => bt) > end > > Thanks for response.In Rails 3, a lot of the query stuff is lazy-loaded, so you may not actually be getting the query you think here. Try it out in rails console, or just look at your development log to see the actual SQL that''s being generated. Arel waits until the last possible second before actually issuing a query, so it may be digging out only the records you need, rather than getting all of them and winnowing down. Walter -- 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.
Frederick Cheung
2011-Jun-25 09:47 UTC
Re: instantiating object and passing a new array into the block
On Jun 24, 7:42 pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote:> Hey all, > > Out of curiousity, all in Rails grabs all the records and converts > them into an array. Map then iterates through the returned array, > returning a new array. So why even use map here? And then the array > gets passed as local variable bt. And we instantiate object and assign > the bt array as a value of a hash of book_type. I dont see why map is > needed here. > > book_details = BookType.all.map do |bt| > Student.new(:book_type => bt) > endSo what do you think this could be simplified to if the use of map is omitted? Fred> > Thanks for response.-- 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.