How to optimise the code

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.

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs