{"action":"created","issue":{"url":"https://api.github.com/repos/dockyard/postgres_ext/issues/163","labels_url":"https://api.github.com/repos/dockyard/postgres_ext/issues/163/labels{/name}","comments_url":"https://api.github.com/repos/dockyard/postgres_ext/issues/163/comments","events_url":"https://api.github.com/repos/dockyard/postgres_ext/issues/163/events","html_url":"https://github.com/dockyard/postgres_ext/issues/163","id":72489033,"number":163,"title":"[Question] Reference a with from another with?","user":{"login":"shipstar","id":115918,"avatar_url":"https://avatars.githubusercontent.com/u/115918?v=3","gravatar_id":"","url":"https://api.github.com/users/shipstar","html_url":"https://github.com/shipstar","followers_url":"https://api.github.com/users/shipstar/followers","following_url":"https://api.github.com/users/shipstar/following{/other_user}","gists_url":"https://api.github.com/users/shipstar/gists{/gist_id}","starred_url":"https://api.github.com/users/shipstar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shipstar/subscriptions","organizations_url":"https://api.github.com/users/shipstar/orgs","repos_url":"https://api.github.com/users/shipstar/repos","events_url":"https://api.github.com/users/shipstar/events{/privacy}","received_events_url":"https://api.github.com/users/shipstar/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-05-01T18:19:42Z","updated_at":"2015-05-01T18:26:36Z","closed_at":null,"body":"I'm trying to write a more complex version of the following query:\r\n\r\n    Business \\\r\n      .with(available_products: Product.for_category(category_id))\r\n      .with(sale_products: available_products.on_sale)\r\n      .find_by_sql(<<-EOSQL\r\n        SELECT *\r\n        FROM businesses\r\n        INNER JOIN available_products ON available_products.business_id = businesses.id\r\n        LEFT JOIN sale_products ON sale_products.business_id = businesses.id\r\n      EOSQL\r\n\r\nIn my hypothetical example here, I want to find businesses with available products for a particular category, but only return the products that are on sale in my JSON. The above doesn't work because available_products is (unsurprisingly) undefined.\r\n\r\nIs it possible to references the `available_products` `with` in the `sale_products` `with` somehow like I can in raw SQL (without just writing SQL for the second `with`)?"},"comment":{"url":"https://api.github.com/repos/dockyard/postgres_ext/issues/comments/98198258","html_url":"https://github.com/dockyard/postgres_ext/issues/163#issuecomment-98198258","issue_url":"https://api.github.com/repos/dockyard/postgres_ext/issues/163","id":98198258,"user":{"login":"danmcclain","id":144138,"avatar_url":"https://avatars.githubusercontent.com/u/144138?v=3","gravatar_id":"","url":"https://api.github.com/users/danmcclain","html_url":"https://github.com/danmcclain","followers_url":"https://api.github.com/users/danmcclain/followers","following_url":"https://api.github.com/users/danmcclain/following{/other_user}","gists_url":"https://api.github.com/users/danmcclain/gists{/gist_id}","starred_url":"https://api.github.com/users/danmcclain/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danmcclain/subscriptions","organizations_url":"https://api.github.com/users/danmcclain/orgs","repos_url":"https://api.github.com/users/danmcclain/repos","events_url":"https://api.github.com/users/danmcclain/events{/privacy}","received_events_url":"https://api.github.com/users/danmcclain/received_events","type":"User","site_admin":false},"created_at":"2015-05-01T18:26:36Z","updated_at":"2015-05-01T18:26:36Z","body":"Is there a reason you don't chain `on_sale` on the original CTE?"}}