Groups
Groups
Sign in
Groups
Groups
Ruby on Rails: Talk
Conversations
About
Send feedback
Help
How to optimise the code
16 views
Skip to first unread message
Hem Chan
unread,
Jun 7, 2016, 9:06:55 PM
6/7/16
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to rubyonra...@googlegroups.com
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
--
Posted via
http://www.ruby-forum.com/
.
Greg Navis
unread,
Jun 8, 2016, 12:25:58 AM
6/8/16
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to rubyonra...@googlegroups.com
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
Hem Chan
unread,
Jun 8, 2016, 12:31:50 AM
6/8/16
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to rubyonra...@googlegroups.com
Thanks very much..
Reply all
Reply to author
Forward
0 new messages