Skip to content

Instantly share code, notes, and snippets.

@shekibobo
Last active September 20, 2018 08:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shekibobo/6729255 to your computer and use it in GitHub Desktop.
Save shekibobo/6729255 to your computer and use it in GitHub Desktop.
Illustrates the issue described in [#9517](https://github.com/rails/rails/issues/9517).
unless File.exists?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :groups do |t|
end
create_table :events do |t|
t.references :group
t.references :user
t.datetime :starts_at
end
create_table :users do |t|
end
end
class Group < ActiveRecord::Base
has_many :events, -> { includes(:user) }
has_many :users, -> { uniq }, through: :events
end
class Event < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
class User < ActiveRecord::Base
has_many :events, -> { includes(:group) }
has_many :groups, through: :events
end
class BugTest < Minitest::Test
def test_association_stuff
group = Group.create!
user = User.create!
group.events << Event.create!(starts_at: DateTime.current, user_id: user.id)
assert_equal 1, group.users.count
assert_equal 1, Event.count
assert_equal group.id, Event.first.group.id
assert_equal 1, user.groups.count
assert_equal user.id, Event.first.user.id
end
end
@shekibobo
Copy link
Author

1) Error:
BugTest#test_association_stuff:
ActiveRecord::ConfigurationError: Association named 'user' was not found on User; perhaps you misspelled it?
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/associations/join_dependency.rb:138:in `build'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/associations/join_dependency.rb:149:in `block in build'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/associations/join_dependency.rb:148:in `each'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/associations/join_dependency.rb:148:in `build'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/associations/join_dependency.rb:39:in `initialize'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/relation/finder_methods.rb:256:in `new'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/relation/finder_methods.rb:256:in `construct_join_dependency'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/relation/finder_methods.rb:260:in `construct_relation_for_association_calculations'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/relation/calculations.rb:99:in `calculate'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/relation/calculations.rb:24:in `count'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/associations/collection_association.rb:209:in `count'
    ~/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails-287014d7249c/activerecord/lib/active_record/associations/collection_proxy.rb:673:in `count'
    has_many_through_with_includes_test.rb:57:in `test_association_stuff'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment