我试图在Rails 3.2.15中的两个模型之间创建一个has-and-owner-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
这是运行&#34; rake db:migrate&#34;:
后的终端输出== 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)>'
答案 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
关联。