{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/17887","labels_url":"https://api.github.com/repos/rails/rails/issues/17887/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/17887/comments","events_url":"https://api.github.com/repos/rails/rails/issues/17887/events","html_url":"https://github.com/rails/rails/issues/17887","id":50714366,"number":17887,"title":"Wrong instance object passed to lambda on has_many :through","user":{"login":"haruska","id":2803,"avatar_url":"https://avatars.githubusercontent.com/u/2803?v=3","gravatar_id":"","url":"https://api.github.com/users/haruska","html_url":"https://github.com/haruska","followers_url":"https://api.github.com/users/haruska/followers","following_url":"https://api.github.com/users/haruska/following{/other_user}","gists_url":"https://api.github.com/users/haruska/gists{/gist_id}","starred_url":"https://api.github.com/users/haruska/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/haruska/subscriptions","organizations_url":"https://api.github.com/users/haruska/orgs","repos_url":"https://api.github.com/users/haruska/repos","events_url":"https://api.github.com/users/haruska/events{/privacy}","received_events_url":"https://api.github.com/users/haruska/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/stale","name":"stale","color":"444444"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2014-12-02T20:27:25Z","updated_at":"2015-06-25T21:59:19Z","closed_at":null,"body":"See the active_record_master.rb test below for details. You have a has_many :through and a lambda (class A.) Another object attempts to access that relationship through another has_many (class D.)\r\n\r\nThe result is an instance of class D getting passed into the lambda.\r\n\r\nThe *expected* result would be an instance of *class A* getting passed into the lambda.\r\n\r\n**active_record_master.rb**\r\n```ruby \r\nunless File.exist?('Gemfile')\r\n  File.write('Gemfile', <<-GEMFILE)\r\n    source 'https://rubygems.org'\r\n    gem 'rails', github: 'rails/rails'\r\n    gem 'arel', github: 'rails/arel'\r\n    gem 'rack', github: 'rack/rack'\r\n    gem 'i18n', github: 'svenfuchs/i18n'\r\n    gem 'sqlite3'\r\n  GEMFILE\r\n\r\n  system 'bundle'\r\nend\r\n\r\nrequire 'bundler'\r\nBundler.setup(:default)\r\n\r\nrequire 'active_record'\r\nrequire 'minitest/autorun'\r\nrequire 'logger'\r\n\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 :as do |t|\r\n    t.integer :b_id\r\n    t.integer :d_id\r\n    t.string :a_value\r\n  end\r\n\r\n  create_table :bs do |t|\r\n    t.string :b_value\r\n  end\r\n\r\n  create_table :cs do |t|\r\n    t.integer :b_id\r\n    t.string :c_value\r\n  end\r\n\r\n  create_table :ds do |t|\r\n    t.string :d_value\r\n  end\r\nend\r\n\r\nclass A < ActiveRecord::Base\r\n  belongs_to :b\r\n  belongs_to :d\r\n  has_many :cs, ->(a) { where(c_value: a.a_value) }, through: :b\r\nend\r\n\r\nclass B < ActiveRecord::Base\r\n  has_many :as\r\n  has_many :cs\r\nend\r\n\r\nclass C < ActiveRecord::Base\r\n  belongs_to :b\r\nend\r\n\r\nclass D < ActiveRecord::Base\r\n  has_many :as\r\n\r\n  # FIXME: Broken in ActiveRecord\r\n  has_many :cs, through: :as\r\nend\r\n\r\nclass BugTest < Minitest::Test\r\n  def test_has_many_through_proc\r\n    d = D.create!\r\n    a = A.create!(d: d)\r\n\r\n    assert_equal a.cs, d.cs\r\n  end\r\nend\r\n```\r\n\r\n**output**\r\n```\r\nFinished in 0.023807s, 42.0045 runs/s, 0.0000 assertions/s.\r\n\r\n  1) Error:\r\nBugTest#test_has_many_through_proc:\r\nNoMethodError: undefined method `a_value' for #<D id: 1, d_value: nil>\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activemodel/lib/active_model/attribute_methods.rb:435:in `method_missing'\r\n    ./active_record_master.rb:49:in `block in <class:A>'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:190:in `instance_exec'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:190:in `eval_scope'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:155:in `block (2 levels) in add_constraints'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:154:in `each'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:154:in `block in add_constraints'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:141:in `each'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:141:in `each_with_index'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:141:in `add_constraints'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:39:in `scope'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association_scope.rb:5:in `scope'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association.rb:97:in `association_scope'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/association.rb:86:in `scope'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/collection_association.rb:413:in `scope'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/collection_proxy.rb:36:in `initialize'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/relation/delegation.rb:106:in `new'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/relation/delegation.rb:106:in `create'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/collection_association.rb:41:in `reader'\r\n    /Users/haruska/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/bundler/gems/rails-3317d6958cf5/activerecord/lib/active_record/associations/builder/association.rb:115:in `cs'\r\n    ./active_record_master.rb:73:in `test_has_many_through_proc'\r\n\r\n1 runs, 0 assertions, 0 failures, 1 errors, 0 skips\r\n```"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/115412464","html_url":"https://github.com/rails/rails/issues/17887#issuecomment-115412464","issue_url":"https://api.github.com/repos/rails/rails/issues/17887","id":115412464,"user":{"login":"rails-bot","id":7468109,"avatar_url":"https://avatars.githubusercontent.com/u/7468109?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-bot","html_url":"https://github.com/rails-bot","followers_url":"https://api.github.com/users/rails-bot/followers","following_url":"https://api.github.com/users/rails-bot/following{/other_user}","gists_url":"https://api.github.com/users/rails-bot/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-bot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-bot/subscriptions","organizations_url":"https://api.github.com/users/rails-bot/orgs","repos_url":"https://api.github.com/users/rails-bot/repos","events_url":"https://api.github.com/users/rails-bot/events{/privacy}","received_events_url":"https://api.github.com/users/rails-bot/received_events","type":"User","site_admin":false},"created_at":"2015-06-25T21:59:19Z","updated_at":"2015-06-25T21:59:19Z","body":"This issue has been automatically marked as stale because it has not been commented on for at least\nthree months.\n\nThe resources of the Rails team are limited, and so we are asking for your help.\n\nIf you can still reproduce this error on the `4-2-stable`, `4-1-stable` branches or on `master`,\nplease reply with all of the information you have about it in order to keep the issue open.\n\nThank you for all your contributions.\n"}}