{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/20628","labels_url":"https://api.github.com/repos/rails/rails/issues/20628/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/20628/comments","events_url":"https://api.github.com/repos/rails/rails/issues/20628/events","html_url":"https://github.com/rails/rails/issues/20628","id":89398672,"number":20628,"title":"Eager loading scoped conditions on remapped table names","user":{"login":"foeken","id":13864,"avatar_url":"https://avatars.githubusercontent.com/u/13864?v=3","gravatar_id":"","url":"https://api.github.com/users/foeken","html_url":"https://github.com/foeken","followers_url":"https://api.github.com/users/foeken/followers","following_url":"https://api.github.com/users/foeken/following{/other_user}","gists_url":"https://api.github.com/users/foeken/gists{/gist_id}","starred_url":"https://api.github.com/users/foeken/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/foeken/subscriptions","organizations_url":"https://api.github.com/users/foeken/orgs","repos_url":"https://api.github.com/users/foeken/repos","events_url":"https://api.github.com/users/foeken/events{/privacy}","received_events_url":"https://api.github.com/users/foeken/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-18T21:10:01Z","updated_at":"2015-06-18T22:03:04Z","closed_at":"2015-06-18T22:03:04Z","body":"I am unsure if this is a bug or just me not knowing the correct Arel syntax to accomplish this so please stay with me for a few minutes while I explain.\r\n\r\nSay we have two AR classes. `Node` and `Property`. `Node` has another `Node` as a parent. And each `Node` has many `Property` objects:\r\n\r\n```ruby\r\nclass Node < ActiveRecord::Base\r\n  belongs_to :parent, class_name: \"Node\"\r\n  has_many :properties, -> { test_scope }\r\nend\r\n\r\nclass Property < ActiveRecord::Base\r\n  belongs_to :node\r\n  scope :test_scope, -> { where(self.arel_table[:key].eq(\"test\")) }\r\nend\r\n```\r\n\r\nHow do I define a (complex) scope on my has_many relationship that survives remapping though eager loading?\r\n\r\n```ruby\r\nNode.eager_load( :properties, :parent => :properties )\r\n```\r\n\r\nyields\r\n\r\n```sql\r\nSELECT \"nodes\".\"id\" \r\n[...] \r\n\"properties_nodes\" ON \"properties_nodes\".\"node_id\" = \"parents_nodes\".\"id\" \r\nAND \"properties\".\"key\" = 'test'\r\n```\r\n\r\n> NOTE: the last part: `AND \"properties\".\"key\" = 'test'` should be `AND \"properties_nodes\".\"key\" = 'test'` as it has been remapped by the eager loading.\r\n\r\nNow for simple scopes this works:\r\n\r\n```ruby\r\nscope :test_scope, -> { where(key: \"test\") }\r\n```\r\n\r\nBut when I need I range query ( like gt or lt ) how I achieve this? Normally you could do the string based version:\r\n\r\n```ruby\r\nscope :test_scope, -> { where(\"date > ?\", Date.today) }\r\n```\r\n\r\nBut that does not get remapped (quite logically). \r\n\r\n```ruby\r\nscope :test_scope, -> { where(self.arel_table[:date].gt(Date.today)) }\r\n```\r\n\r\nWhen using Arel I would expect it to get remapped to the proper join table name. **How do I achieve this, or is this a bug and should the Arel code have been remapped by rails?**"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/113301273","html_url":"https://github.com/rails/rails/issues/20628#issuecomment-113301273","issue_url":"https://api.github.com/repos/rails/rails/issues/20628","id":113301273,"user":{"login":"matthewd","id":1034,"avatar_url":"https://avatars.githubusercontent.com/u/1034?v=3","gravatar_id":"","url":"https://api.github.com/users/matthewd","html_url":"https://github.com/matthewd","followers_url":"https://api.github.com/users/matthewd/followers","following_url":"https://api.github.com/users/matthewd/following{/other_user}","gists_url":"https://api.github.com/users/matthewd/gists{/gist_id}","starred_url":"https://api.github.com/users/matthewd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matthewd/subscriptions","organizations_url":"https://api.github.com/users/matthewd/orgs","repos_url":"https://api.github.com/users/matthewd/repos","events_url":"https://api.github.com/users/matthewd/events{/privacy}","received_events_url":"https://api.github.com/users/matthewd/received_events","type":"User","site_admin":false},"created_at":"2015-06-18T22:03:04Z","updated_at":"2015-06-18T22:03:04Z","body":" Please use the mailing list or StackOverflow for questions/help, where a wider community will be able to help you. We reserve the issues tracker for issues only."}}