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)
Greg Navis
I help tech companies to scale Heroku-hosted Rails apps.
Free, biweekly scalability newsletter for SaaS CEOs
http://www.gregnavis.com/newsletter/
Thanks very much…
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs