How would I optimize the following code?
def posts_for_30_blogs blogs = Blog.limit(30) blogs.flat_map do |blog| blog.posts.to_a end end
How would I optimize the following code?
def posts_for_30_blogs blogs = Blog.limit(30) blogs.flat_map do |blog| blog.posts.to_a end end
Optimise in what sense? If this is about solving the N + 1 query problem then try:
blogs = Blog.include(:posts).limit(30)
This should load associated posts with only 1 query.
Thanks very much..