Skip to content

Instantly share code, notes, and snippets.

@Epigene Epigene/Loop over users.rb
Last active Jul 29, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.