Inside Rails可安装引擎我生成了一个模型:
rails g model Sources domain:string source_alias:string is_social:boolean is_organic:boolean organic_query_param:string
这是迁移文件(20131220160128_create_sourcebuster_sources.rb
)
class CreateSourcebusterSources < ActiveRecord::Migration
def change
create_table :sourcebuster_sources do |t|
t.string :domain, null: false
t.string :source_alias
t.boolean :is_social, default: false
t.boolean :is_organic, default: false
t.string :organic_query_param
t.timestamps
end
add_index :sourcebuster_sources, :domain, unique: true
add_index :sourcebuster_sources, :is_social
add_index :sourcebuster_sources, :is_organic
Sourcebuster::Source.create domain: "yahoo.com",
source_alias: "yahoo.com",
is_social: false,
is_organic: true,
organic_query_param: "p"
end
end
将其复制到虚拟应用程序:
rake sourcebuster:install:migrations
具有不同名称的迁移文件 - 20131220164445_create_sourcebuster_sources.sourcebuster.rb
- 但内容与上述内容相同。
好的,bundle exec rake db:migrate
在尝试插入数据行时,它给了我一个错误:
== CreateSourcebusterSources: migrating ======================================
-- create_table(:sourcebuster_sources)
-> 0.0364s
-- add_index(:sourcebuster_sources, :domain, {:unique=>true})
-> 0.0066s
-- add_index(:sourcebuster_sources, :is_social)
-> 0.0069s
-- add_index(:sourcebuster_sources, :is_organic)
-> 0.0046s
rake aborted!
An error has occurred, this and all later migrations canceled:
uninitialized constant Sourcebuster::Source/Users/Alex/Ruby/sourcebuster/test/dummy/db/migrate/20131220164445_create_sourcebuster_sources.sourcebuster.rb:17:in `change'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:571:in `exec_migration'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/bin/ruby_noexec_wrapper:14:in `eval'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353@sourcebuster/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
为什么插入失败?
答案 0 :(得分:0)
尝试插入此方法
class CreateSourcebusterSources < ActiveRecord::Migration
def migrate(direction)
super
# Create a default Sourcebuster
Sourcebuster::Source.create! domain: "yahoo.com",
source_alias: "yahoo.com",
is_social: false,
is_organic: true,
organic_query_param: "p" if direction == :up
end
#def change
...
end