Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Epigene
Last active July 29, 2017 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Epigene/d5a3155755cd16d6f71b22d59913a16f to your computer and use it in GitHub Desktop.
Save Epigene/d5a3155755cd16d6f71b22d59913a16f to your computer and use it in GitHub Desktop.
The ugly
last_posts = []
User.all.each |user|
last_posts << user.posts.order(created_at: :asc).last
end
# oops, some users have no posts and the array has nils
last_posts = last_posts.compact
# now to have them in correct order
last_posts = last_posts.sort{ |x, y| y.created_at <=> x.created_at }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment