This project is archived and is in readonly mode.

#4230 ✓resolved
phs

[PATCH] AR migration generator includes model's modules in table name.

Reported by phs | March 19th, 2010 @ 05:00 AM | in 3.0.2

Below, the generated migration creates the table "my_module_posts". AR expects MyModule::Post to use a table named "posts".

After reading #1976 and #2965, it seems AR's table name is the correct one.

Wheelie-Cyberman:~ $ rails --version
Rails 3.0.0.beta
Wheelie-Cyberman:~ $ rails blog
      create  
      create  README
      # ...
Wheelie-Cyberman:~ $ cd blog
Wheelie-Cyberman:~/blog $ rails g model my_module/post title:string body:text
      invoke  active_record
      create    db/migrate/20100319042749_create_my_module_posts.rb
      create    app/models/my_module/post.rb
      invoke    test_unit
      create      test/unit/my_module/post_test.rb
      create      test/fixtures/my_module_posts.yml
Wheelie-Cyberman:~/blog $ cat db/migrate/20100319042749_create_my_module_posts.rb 
class CreateMyModulePosts < ActiveRecord::Migration
  def self.up
    create_table :my_module_posts do |t|
      t.string :title
      t.text :body

      t.timestamps
    end
  end

  def self.down
    drop_table :my_module_posts
  end
end
Wheelie-Cyberman:~/blog $ rake db:migrate
(in /Users/phil/blog)
==  CreateMyModulePosts: migrating ============================================
-- create_table(:my_module_posts)
   -> 0.0015s
==  CreateMyModulePosts: migrated (0.0016s) ===================================

Wheelie-Cyberman:~/blog $ rails c
Loading development environment (Rails 3.0.0.beta)
>> MyModule::Post.all
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: posts: SELECT     "posts".* FROM       "posts"
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/connection_adapters/abstract_adapter.rb:206:in `log'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/connection_adapters/sqlite_adapter.rb:150:in `execute'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/connection_adapters/sqlite_adapter.rb:390:in `catch_schema_changes'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/connection_adapters/sqlite_adapter.rb:150:in `execute'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/connection_adapters/sqlite_adapter.rb:293:in `select'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/base.rb:586:in `find_by_sql'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/relation.rb:49:in `to_a'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/relation/finder_methods.rb:119:in `all'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/base.rb:560:in `__send__'
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta/lib/active_record/base.rb:560:in `all'
    from (irb):1

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages