繁体   English   中英

Rails 3.2.15迁移失败:未定义的方法`create_join_table'

[英]Rails 3.2.15 migration fails: undefined method `create_join_table'

我正在尝试在Rails 3.2.15中的两个模型之间创建一个has-and-belongs-to-many-many富连接。 我在这做错了什么?

这是我的迁移代码:

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)>'

在rails 4.0.2中添加了create_join_table 您需要使用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关联。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM