Skip to content

Instantly share code, notes, and snippets.

@christophermanning
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophermanning/9b1220293b70fc71968b to your computer and use it in GitHub Desktop.
Save christophermanning/9b1220293b70fc71968b to your computer and use it in GitHub Desktop.
Rails ActiveRecord 4.1.2.rc2 Namespaced HABTM Regression
~  ruby habtm.rb
Active Record 4.1.1
-- create_table(:articles, {:force=>true})
   -> 0.3158s
-- create_table(:publisher_magazines, {:force=>true})
   -> 0.0005s
-- create_table(:articles_publisher_magazines, {:force=>true})
   -> 0.0005s
#<ActiveRecord::Associations::CollectionProxy [#<Article id: 1>]>~  ruby habtm.rb
Active Record 4.1.2.rc2
-- create_table(:articles, {:force=>true})
   -> 0.3163s
-- create_table(:publisher_magazines, {:force=>true})
   -> 0.0005s
-- create_table(:articles_publisher_magazines, {:force=>true})
   -> 0.0005s
/home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activesupport-4.1.2.rc2/lib/active_support/inflector/methods.rb:253:in `const_get': uninitialized constant Publisher::Article (NameError)
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activesupport-4.1.2.rc2/lib/active_support/inflector/methods.rb:253:in `block in constantize'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activesupport-4.1.2.rc2/lib/active_support/inflector/methods.rb:236:in `each'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activesupport-4.1.2.rc2/lib/active_support/inflector/methods.rb:236:in `inject'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activesupport-4.1.2.rc2/lib/active_support/inflector/methods.rb:236:in `constantize'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activesupport-4.1.2.rc2/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/builder/has_and_belongs_to_many.rb:18:in `klass'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/builder/has_and_belongs_to_many.rb:14:in `join_table'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/builder/has_and_belongs_to_many.rb:60:in `table_name'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/core.rb:151:in `arel_table'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/core.rb:167:in `relation'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/scoping/named.rb:33:in `default_scoped'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/scoping/named.rb:28:in `all'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/through_association.rb:17:in `block in target_scope'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/through_association.rb:16:in `each'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/through_association.rb:16:in `target_scope'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/association.rb:86:in `scope'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/collection_association.rb:404:in `scope'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/collection_proxy.rb:36:in `initialize'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/relation/delegation.rb:106:in `new'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/relation/delegation.rb:106:in `create'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/collection_association.rb:36:in `reader'
        from /home/cmanning/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2.rc2/lib/active_record/associations/builder/association.rb:110:in `articles'
        from habtm.rb:49:in `<main>'
# this example works as written in rails 4.1.1
#gem 'rails', '4.1.1'
gem 'rails', '4.1.2.rc2'
require 'active_record'
# Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"
# Connect to an in-memory sqlite3 database (more on this in a moment)
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)
# Create the minimal database schema necessary to reproduce the bug
ActiveRecord::Schema.define do
create_table :articles, :force => true do |t|
end
create_table :publisher_magazines, :force => true do |t|
end
create_table :articles_publisher_magazines, force: true do |t|
t.integer :article_id
t.integer :publisher_magazine_id
end
end
# Create the minimal set of models to reproduce the bug
module Publisher
def self.table_name_prefix
'publisher_'
end
end
class Article < ActiveRecord::Base
has_and_belongs_to_many :publisher_magazines, class_name: 'Publisher::Magazine', association_foreign_key: 'publisher_magazine_id'
end
class Publisher::Magazine < ActiveRecord::Base
has_and_belongs_to_many :articles, foreign_key: 'publisher_magazine_id'
# adding this line fixes the uninitialized constant exception
#, class_name: 'Article'
end
# Create some test data
magazine = Publisher::Magazine.create
article = Article.create
magazine.articles << article
magazine.save!
# exception: uninitialized constant Publisher::Article (NameError)
p Publisher::Magazine.where(id: magazine.id).first.articles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment