{"action":"created","issue":{"url":"https://api.github.com/repos/spree/spree/issues/7031","repository_url":"https://api.github.com/repos/spree/spree","labels_url":"https://api.github.com/repos/spree/spree/issues/7031/labels{/name}","comments_url":"https://api.github.com/repos/spree/spree/issues/7031/comments","events_url":"https://api.github.com/repos/spree/spree/issues/7031/events","html_url":"https://github.com/spree/spree/pull/7031","id":132626429,"number":7031,"title":"Fix n+1 queries in frontend app - HomeController#index for promotions","user":{"login":"Bharat311","id":1005808,"avatar_url":"https://avatars.githubusercontent.com/u/1005808?v=3","gravatar_id":"","url":"https://api.github.com/users/Bharat311","html_url":"https://github.com/Bharat311","followers_url":"https://api.github.com/users/Bharat311/followers","following_url":"https://api.github.com/users/Bharat311/following{/other_user}","gists_url":"https://api.github.com/users/Bharat311/gists{/gist_id}","starred_url":"https://api.github.com/users/Bharat311/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Bharat311/subscriptions","organizations_url":"https://api.github.com/users/Bharat311/orgs","repos_url":"https://api.github.com/users/Bharat311/repos","events_url":"https://api.github.com/users/Bharat311/events{/privacy}","received_events_url":"https://api.github.com/users/Bharat311/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-02-10T07:26:15Z","updated_at":"2016-02-18T10:29:41Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/spree/spree/pulls/7031","html_url":"https://github.com/spree/spree/pull/7031","diff_url":"https://github.com/spree/spree/pull/7031.diff","patch_url":"https://github.com/spree/spree/pull/7031.patch"},"body":"In the frontend app - on main homepage (HomeController#index) - there are a couple of promotion_rule and promotion queries being fired for each Spree::Product. The reason can be found [here](https://github.com/spree/spree/blob/master/core/app/helpers/spree/products_helper.rb#L62).\r\n\r\nFixed it by making the *possible_promotions* a through relation on Spree::Product so that it can be  eager-loaded while loading products.\r\n\r\nSimilarly, fixed for ProductsController#index as well.\r\n"},"comment":{"url":"https://api.github.com/repos/spree/spree/issues/comments/185643982","html_url":"https://github.com/spree/spree/pull/7031#issuecomment-185643982","issue_url":"https://api.github.com/repos/spree/spree/issues/7031","id":185643982,"user":{"login":"Mafi88","id":643748,"avatar_url":"https://avatars.githubusercontent.com/u/643748?v=3","gravatar_id":"","url":"https://api.github.com/users/Mafi88","html_url":"https://github.com/Mafi88","followers_url":"https://api.github.com/users/Mafi88/followers","following_url":"https://api.github.com/users/Mafi88/following{/other_user}","gists_url":"https://api.github.com/users/Mafi88/gists{/gist_id}","starred_url":"https://api.github.com/users/Mafi88/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mafi88/subscriptions","organizations_url":"https://api.github.com/users/Mafi88/orgs","repos_url":"https://api.github.com/users/Mafi88/repos","events_url":"https://api.github.com/users/Mafi88/events{/privacy}","received_events_url":"https://api.github.com/users/Mafi88/received_events","type":"User","site_admin":false},"created_at":"2016-02-18T10:29:40Z","updated_at":"2016-02-18T10:29:40Z","body":"@Bharat311 You can do it like this (which I think is easiest to read):\r\n\r\n```\r\nhas_many :possible_promotions, -> { advertised.active }, through: :promotion_rules,\r\n                                                         class_name: 'Spree::Promotion',\r\n                                                         source: :promotion\r\n```\r\nor if you prefer splitting it into multiple lines, you can do:\r\n\r\n```\r\nhas_many :possible_promotions,\r\n         -> { advertised.active },\r\n         through: :promotion_rules,\r\n         class_name: 'Spree::Promotion',\r\n         source: :promotion\r\n```\r\n\r\nlike @priyank-gupta suggested.\r\n\r\nPlease rebase with latest master."}}