Rails 3.2.15 迁移失败:undefined method `create_join_table'。

4

我正在尝试在Rails 3.2.15中创建两个模型之间的多对多关系,但是出现了问题。这里有我的迁移代码:

class CreateTopicInterest < ActiveRecord::Migration
  def change
    create_join_table :users, :topics, table_name: :topic_interest do |t|
      t.index :user_id
      t.index :topic_id
      t.integer :interest_type

      t.timestamps
    end
  end
end

运行 "rake db:migrate" 后,这是终端输出的结果:

==  CreateTopicInterest: migrating ============================================
-- create_join_table(:users, :topics, {:table_name=>:topic_interest})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `create_join_table' for #<CreateTopicInterest:0x007fe7ac8d86a0>/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:465:in `block in method_missing'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:438:in `block in say_with_time'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:438:in `say_with_time'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:458:in `method_missing'
/Users/duncanmalashock/rails_projects/diver/db/migrate/20140801151401_create_topic_interest.rb:3:in `change'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:407:in `block in migrate'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:389:in `migrate'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:528:in `migrate'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:775:in `call'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/transactions.rb:208:in `transaction'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:775:in `ddl_transaction'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:719:in `block in migrate'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:700:in `each'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:700:in `migrate'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:570:in `up'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/migration.rb:551:in `migrate'
/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448@diver/gems/activerecord-3.2.15/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
1个回答

3

create_join_table在Rails 4.0.2中已经被添加。您需要使用create_table代替。

create_table :topic_interest, id: false do
  t.integer :user_id
  t.integer :topic_id

  t.index :user_id
  t.index :topic_id
end

请注意,使用habtm关联将不允许您在关联上使用任何额外的字段(例如您想要添加的interest_type)。相反,请使用has_many :through关联。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接