{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/22723","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/22723/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/22723/comments","events_url":"https://api.github.com/repos/rails/rails/issues/22723/events","html_url":"https://github.com/rails/rails/issues/22723","id":123269223,"number":22723,"title":"Activerecord scope chaining error when using same columns","user":{"login":"valgayres","id":15716087,"avatar_url":"https://avatars.githubusercontent.com/u/15716087?v=3","gravatar_id":"","url":"https://api.github.com/users/valgayres","html_url":"https://github.com/valgayres","followers_url":"https://api.github.com/users/valgayres/followers","following_url":"https://api.github.com/users/valgayres/following{/other_user}","gists_url":"https://api.github.com/users/valgayres/gists{/gist_id}","starred_url":"https://api.github.com/users/valgayres/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/valgayres/subscriptions","organizations_url":"https://api.github.com/users/valgayres/orgs","repos_url":"https://api.github.com/users/valgayres/repos","events_url":"https://api.github.com/users/valgayres/events{/privacy}","received_events_url":"https://api.github.com/users/valgayres/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-12-21T12:20:03Z","updated_at":"2016-05-22T08:51:12Z","closed_at":null,"body":"Hi, \r\n\r\nI've encountered a strange behavior when chaining scopes that act on the same columns after migrating from rails 4.0.0 to 4.2.4.\r\n\r\nLet's consider the following class\r\n```\r\nclass  Car < ActiveRecord::Base\r\n  scope :electric_car, -> { where(electric: true, sold: false) }\r\n  scope :for_owner, ->(owner_id) { where(owner_id: owner_id, sold: false) }\r\nend\r\n```\r\n\r\nNow, theoretically, doing `Car.for_owner(1).electric_car` and `Car.electric_car.for_owner(1)` should give the same answer.\r\n\r\nThe problem is that looking at the sql query we see the following issue:\r\n\r\n```\r\nCar.for_owner(1).electric_car.to_sql\r\n=> \"Select \\\"cars\\\".* FROM \\\"cars\\\" WHERE \\\"cars\\\".\\\"owner_id\\\" = 1 AND \\\"cars\\\".\\\"sold\\\" = 'f' AND \\\"cars\\\".\\\"sold\\\" = 't' AND \\\"cars\\\".\\\"electric\\\" = 'f'\"\r\n\r\nCar.electric_car.for_owner(1).to_sql\r\n=> Select \\\"cars\\\".* FROM \\\"cars\\\" WHERE\\\"cars\\\".\\\"electric\\\" = 't' AND \\\"cars\\\".\\\"sold\\\" = 'f' AND \\\"cars\\\".\\\"sold\\\" = 'f' AND  \\\"cars\\\".\\\"owner_id\\\" = 1\"\r\n```\r\n\r\nAs we can see, In the first case, the second filter on the \"sold\" column and the filter on the \"electric\" column are switched. \r\nAnalysing a bit the sql queries, we understood that the issue is that parameters of the query are reordered, but not the values asked. For example, running real queries in development, we see this:\r\n\r\n```\r\nCar.for_owner(1).electric_car\r\nCar load (2.0ms) Select \"cars\".* FROM \"cars\" WHERE \"cars\".\"owner_id\" = 1 AND \"cars\".\"sold\" = $1 AND \"cars\".\"sold\" = $2 AND \"cars\".\"electric\" = $3 [[\"sold\", \"f\"], [\"electric\", \"t\"], [\"sold\", \"false\"]]\r\n=> #<ActiveRecord::Relation []>\r\n```\r\n\r\nHas anyone ever seen this? Or have a fix?\r\n\r\nThanks a lot!"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/220821273","html_url":"https://github.com/rails/rails/issues/22723#issuecomment-220821273","issue_url":"https://api.github.com/repos/rails/rails/issues/22723","id":220821273,"user":{"login":"sevaorlov","id":1062645,"avatar_url":"https://avatars.githubusercontent.com/u/1062645?v=3","gravatar_id":"","url":"https://api.github.com/users/sevaorlov","html_url":"https://github.com/sevaorlov","followers_url":"https://api.github.com/users/sevaorlov/followers","following_url":"https://api.github.com/users/sevaorlov/following{/other_user}","gists_url":"https://api.github.com/users/sevaorlov/gists{/gist_id}","starred_url":"https://api.github.com/users/sevaorlov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sevaorlov/subscriptions","organizations_url":"https://api.github.com/users/sevaorlov/orgs","repos_url":"https://api.github.com/users/sevaorlov/repos","events_url":"https://api.github.com/users/sevaorlov/events{/privacy}","received_events_url":"https://api.github.com/users/sevaorlov/received_events","type":"User","site_admin":false},"created_at":"2016-05-22T08:51:12Z","updated_at":"2016-05-22T08:51:12Z","body":"@valgayres, I've created this script and switching doesn't happen for me in Rails 4.2.4. What am I doing wrong?\r\n\r\n```ruby\r\nbegin\r\n  require 'bundler/inline'\r\nrescue LoadError => e\r\n  $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'\r\n  raise e\r\nend\r\n\r\ngemfile(true) do\r\n  source 'https://rubygems.org'\r\n  gem 'rails', '4.2.4'\r\n  gem 'sqlite3'\r\nend\r\n\r\nrequire 'active_record'\r\nrequire 'minitest/autorun'\r\nrequire 'logger'\r\n\r\n# Ensure backward compatibility with Minitest 4\r\nMinitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)\r\n\r\n# This connection will do for database-independent bug reports.\r\nActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')\r\nActiveRecord::Base.logger = Logger.new(STDOUT)\r\n\r\nActiveRecord::Schema.define do\r\n  create_table :cars, force: true do |t|\r\n    t.boolean :sold\r\n    t.boolean :electric\r\n    t.integer :owner_id\r\n  end\r\nend\r\n\r\nclass Car < ActiveRecord::Base\r\n  scope :electric_car, -> { where(electric: true, sold: false) }\r\n  scope :for_owner, ->(owner_id) { where(owner_id: owner_id, sold: false) }\r\nend\r\n\r\nclass BugTest < Minitest::Test\r\n  def test_scope_chaining\r\n    # \"SELECT \\\"cars\\\".* FROM \\\"cars\\\" WHERE \\\"cars\\\".\\\"owner_id\\\" = 1 AND \\\"cars\\\".\\\"sold\\\" = 'f' AND\r\n    # \\\"cars\\\".\\\"electric\\\" = 't' AND \\\"cars\\\".\\\"sold\\\" = 'f'\"\r\n    p Car.for_owner(1).electric_car.to_sql\r\n\r\n    # \"SELECT \\\"cars\\\".* FROM \\\"cars\\\" WHERE \\\"cars\\\".\\\"electric\\\" = 't' AND \\\"cars\\\".\\\"sold\\\" = 'f' AND\r\n    #\\\"cars\\\".\\\"owner_id\\\" = 1 AND \\\"cars\\\".\\\"sold\\\" = 'f'\"\r\n    p Car.electric_car.for_owner(1).to_sql\r\n  end\r\nend\r\n```"}}
{"actor":{"display_login":"sevaorlov"}}