{"action":"closed","issue":{"url":"https://api.github.com/repos/loicteixeira/kudos/issues/6","repository_url":"https://api.github.com/repos/loicteixeira/kudos","labels_url":"https://api.github.com/repos/loicteixeira/kudos/issues/6/labels{/name}","comments_url":"https://api.github.com/repos/loicteixeira/kudos/issues/6/comments","events_url":"https://api.github.com/repos/loicteixeira/kudos/issues/6/events","html_url":"https://github.com/loicteixeira/kudos/issues/6","id":143851453,"number":6,"title":"Scoping issues in scope :o","user":{"login":"loicteixeira","id":6834938,"avatar_url":"https://avatars.githubusercontent.com/u/6834938?v=3","gravatar_id":"","url":"https://api.github.com/users/loicteixeira","html_url":"https://github.com/loicteixeira","followers_url":"https://api.github.com/users/loicteixeira/followers","following_url":"https://api.github.com/users/loicteixeira/following{/other_user}","gists_url":"https://api.github.com/users/loicteixeira/gists{/gist_id}","starred_url":"https://api.github.com/users/loicteixeira/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/loicteixeira/subscriptions","organizations_url":"https://api.github.com/users/loicteixeira/orgs","repos_url":"https://api.github.com/users/loicteixeira/repos","events_url":"https://api.github.com/users/loicteixeira/events{/privacy}","received_events_url":"https://api.github.com/users/loicteixeira/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/loicteixeira/kudos/labels/question","name":"question","color":"cc317c"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-03-27T21:46:08Z","updated_at":"2016-03-29T19:17:39Z","closed_at":"2016-03-29T19:17:38Z","body":"Implementing a `scope` with the sugar method or the old fashion way does not give the same result.\r\n\r\n```ruby\r\nclass Account < ActiveRecord::Base\r\n  scope :scope_twitter_account, -> { where(type: 'TwitterAccount').first }\r\n\r\n  def self.class_twitter_account\r\n    where(type: 'TwitterAccount').first\r\n  end\r\nend\r\n\r\nclass TwitterAccount < Account\r\nend\r\n\r\nclass GithubAccount < Account\r\nend\r\n```\r\n\r\nWith the `scope` method, it does two queries and returns the result of the second.\r\n```ruby\r\n> Account.scope_twitter_account\r\nAccount Load (0.3ms)  SELECT  \"accounts\".* FROM \"accounts\" WHERE \"accounts\".\"type\" = $1  ORDER BY \"accounts\".\"id\" ASC LIMIT 1  [[\"type\", \"TwitterAccount\"]]\r\nAccount Load (0.4ms)  SELECT \"accounts\".* FROM \"accounts\"\r\n[<GithubAccount:0x007fd7f785b1b0>, <TwitterAccount:0x007fd7f7887da>, <GithubAccount:0x007fd7f785a789>, ...]\r\n```\r\n\r\nWhile the `class` method, it only does one query.\r\n```ruby\r\n> Account.class_twitter_account\r\nAccount Load (0.5ms)  SELECT  \"accounts\".* FROM \"accounts\" WHERE \"accounts\".\"type\" = $1  ORDER BY \"accounts\".\"id\" ASC LIMIT 1  [[\"type\", \"TwitterAccount\"]]\r\n<TwitterAccount:0x007fd7f7887da>\r\n ```\r\n\r\nI first thought it was because `first` is bound to `Account` in the former and to something else in the latter but that doesn't make sense. Can you help me clarify what's going on?"}}