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.

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

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

So what do you think this could be simplified to if the use of map is
omitted?

Fred