{"action":"created","issue":{"url":"https://api.github.com/repos/radar/forem/issues/667","labels_url":"https://api.github.com/repos/radar/forem/issues/667/labels{/name}","comments_url":"https://api.github.com/repos/radar/forem/issues/667/comments","events_url":"https://api.github.com/repos/radar/forem/issues/667/events","html_url":"https://github.com/radar/forem/issues/667","id":94411966,"number":667,"title":"Method to query a group of forums 'last_post_for(forem_user)'","user":{"login":"Buildzzz","id":3992851,"avatar_url":"https://avatars.githubusercontent.com/u/3992851?v=3","gravatar_id":"","url":"https://api.github.com/users/Buildzzz","html_url":"https://github.com/Buildzzz","followers_url":"https://api.github.com/users/Buildzzz/followers","following_url":"https://api.github.com/users/Buildzzz/following{/other_user}","gists_url":"https://api.github.com/users/Buildzzz/gists{/gist_id}","starred_url":"https://api.github.com/users/Buildzzz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Buildzzz/subscriptions","organizations_url":"https://api.github.com/users/Buildzzz/orgs","repos_url":"https://api.github.com/users/Buildzzz/repos","events_url":"https://api.github.com/users/Buildzzz/events{/privacy}","received_events_url":"https://api.github.com/users/Buildzzz/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-07-10T23:21:17Z","updated_at":"2015-07-14T23:46:58Z","closed_at":"2015-07-11T06:45:36Z","body":"Hi,\r\n\r\nI am currently displaying a group of forums (lets say for example 50 forums) and alongside each forum name, I'm calling 'last_post_for(forem_user)' to show the latest post in that forum.\r\n\r\nThe problem is that we have a lot of forums and a lot of visitors to the page.  This is causing a strain and I'm hoping to optimize my code. \r\n\r\nSo instead of querying my database 50 times to get the latest post, is there a way to query all the latest posts for each of my forums in one query and then do an each loop to display the resulting data?\r\n\r\nYour help is greatly appreciated.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/radar/forem/issues/comments/121430207","html_url":"https://github.com/radar/forem/issues/667#issuecomment-121430207","issue_url":"https://api.github.com/repos/radar/forem/issues/667","id":121430207,"user":{"login":"Buildzzz","id":3992851,"avatar_url":"https://avatars.githubusercontent.com/u/3992851?v=3","gravatar_id":"","url":"https://api.github.com/users/Buildzzz","html_url":"https://github.com/Buildzzz","followers_url":"https://api.github.com/users/Buildzzz/followers","following_url":"https://api.github.com/users/Buildzzz/following{/other_user}","gists_url":"https://api.github.com/users/Buildzzz/gists{/gist_id}","starred_url":"https://api.github.com/users/Buildzzz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Buildzzz/subscriptions","organizations_url":"https://api.github.com/users/Buildzzz/orgs","repos_url":"https://api.github.com/users/Buildzzz/repos","events_url":"https://api.github.com/users/Buildzzz/events{/privacy}","received_events_url":"https://api.github.com/users/Buildzzz/received_events","type":"User","site_admin":false},"created_at":"2015-07-14T23:46:58Z","updated_at":"2015-07-14T23:46:58Z","body":"Hi Ryan,\n\nAfter beating my head against the wall, I figured out a solution.  Not sure\nwhere/how you would like for me to integrate it into the gem but here's the\nmethod:\n\ndef lastpostforforums(array_of_forum_ids)\n\nforumids = array_of_forum_ids\n\nlastposts = Forem::Post.joins(:topic).where(topic_id:\nForem::Topic.where(forum_id: forumids, hidden: [false,\nnil])).group(\"forem_topics.forum_id\").maximum(\"forem_posts.created_at\")\n\nlastpostdetails = Forem::Post.joins(:topic, :forem_user).where(created_at:\nlastposts.values).pluck(\"forem_topics.forum_id\", \"forem_topics.subject\",\n\"forem_posts.created_at\", \"users.name\")\n\nend\n\nFYI, the current lastpost method had a bug where it returns hidden topics.\n\nBest,\nTim Zhu\n\nOn Sat, Jul 11, 2015 at 5:37 PM, Ryan Bigg <notifications@github.com> wrote:\n\n> The gem doesn't come with a premade solution for querying multiple forums.\n> If you could come up with one and share it with us, that would be\n> appreciated :)\n>\n> —\n> Reply to this email directly or view it on GitHub\n> <https://github.com/radar/forem/issues/667#issuecomment-120674265>.\n>\n"}}