我正在尝试将列添加到我拥有的模型中(线索)。我在线阅读这与我的迁移文件有关。 这是我的迁移文件:
class Addboolstoclue < ActiveRecord::Migration
def self.up
add_column :clue do |t|
t.boolean :clue1
t.boolean :clue2
t.boolean :clue3
t.boolean :clue4
t.boolean :clue5
t.boolean :clue6
t.boolean :clue7
t.boolean :clue8
t.boolean :clue9
t.boolean :clue10
t.boolean :clue11
t.boolean :clue12
t.boolean :clue13
t.boolean :clue14
t.boolean :clue15
t.boolean :done
t.string :user_id
end
end
end
当我运行migrate,My Terminal:
时会出现此错误rake db:migrate
== 20150418012429 Addboolstoclue: migrating ===================================
-- add_column(:clue)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (1 for 3..4)/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:452:in `add_column'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:649:in `block in method_missing'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:621:in `block in say_with_time'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:621:in `say_with_time'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:641:in `method_missing'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:406:in `method_missing'
/Users/coleschiffer/Code/rp/protected-beyond-7706/db/migrate/20150418012429_addboolstoclue.rb:3:in `up'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:558:in `up'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:598:in `exec_migration'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:578:in `block in migrate'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:577:in `migrate'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:752:in `migrate'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:992:in `block in execute_migration_in_transaction'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:1038:in `block in ddl_transaction'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `block in transaction'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/transactions.rb:208:in `transaction'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:1038:in `ddl_transaction'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:991:in `execute_migration_in_transaction'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:953:in `block in migrate'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:949:in `each'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:949:in `migrate'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:807:in `up'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/migration.rb:785:in `migrate'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.0/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in `eval'
/Users/coleschiffer/.rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:migrate
感谢您的帮助!
答案 0 :(得分:2)
尝试一次添加多个列时,请尝试使用change_table而不是add_column 喜欢:
class Addboolstoclue < ActiveRecord::Migration
def change
change_table :clue do |t|
t.boolean :clue1
t.boolean :clue2
t.boolean :clue3
t.boolean :clue4
t.boolean :clue5
t.boolean :clue6
t.boolean :clue7
t.boolean :clue8
t.boolean :clue9
t.boolean :clue10
t.boolean :clue11
t.boolean :clue12
t.boolean :clue13
t.boolean :clue14
t.boolean :clue15
t.boolean :done
t.string :user_id
end
end
end
答案 1 :(得分:1)
add_column通常一次完成一列,而不是块:
add_column :clue, :clue1, :boolean
add_column :clue, :clue2, :boolean
...
你想要在self.change方法中做到这一点,而不是编写一个up和down方法。 rails指南的这一部分对此进行了更多讨论:
http://guides.rubyonrails.org/active_record_migrations.html#creating-a-standalone-migration