为什么我在迁移过程中会收到错误以及默认数据?
rails g migration CreateStates
然后将以下代码添加到迁移中:
=============================
class CreateStates < ActiveRecord::Migration
def self.up
create_table :states do |t|
t.column :name, :string
t.column :abbreviation, :string
end
State.create :name => 'Alabama', :abbreviation => 'AL'
State.create :name => 'Alaska', :abbreviation => 'AK'
State.create :name => 'Arizona', :abbreviation => 'AZ'
end
def self.down
drop_table :states
end
end
============== ==============
我收到错误:
** Invoke db:migrate (first_time)
** 调用环境(first_time)
** 执行环境
** 执行 db:migrate
== CreateStates: 迁移 ================================================= ======
-- create_table(:states)
-> 0.0010s
耙子中止!
发生错误,此迁移和所有后续迁移均已取消:
未初始化常量 CreateStates::State
/Users/jondoe/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/backward_compatibility.rb:20:in ` const_missing'
========
看起来应该可以做到这一点:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
我还尝试创建一个模型而不仅仅是一个迁移文件。还是同样的错误。我还尝试创建 2 个迁移(一个用于创建表,一个用于添加数据),但这也不起作用。有什么想法吗?
rails g migration CreateStates
Then add the following code to the migration:
===========================
class CreateStates < ActiveRecord::Migration
def self.up
create_table :states do |t|
t.column :name, :string
t.column :abbreviation, :string
end
State.create :name => 'Alabama', :abbreviation => 'AL'
State.create :name => 'Alaska', :abbreviation => 'AK'
State.create :name => 'Arizona', :abbreviation => 'AZ'
end
def self.down
drop_table :states
end
end
============================
I get an error:
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== CreateStates: migrating ===================================================
-- create_table(:states)
-> 0.0010s
rake aborted!
An error has occurred, this and all later migrations canceled:
uninitialized constant CreateStates::State
/Users/jondoe/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
========
It seems like this should be able to do this:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
I have also tried to create a model instead of a just a migration file. Still same error. I have also tried creating 2 migrations (one to create table, then one to add data) and that didn't work either. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
在 State.create 之前
执行以下操作: 文档
Try doing:
before your State.create.
documentation
您的代码将完美运行,除非您实际上没有
State
类。 Rails 了解此类的唯一方法是在 app/models/state.rb 中将其定义为 Class StateClass State
ActiveRecord::Base...
我建议运行以下代码行,而不是运行自定义迁移:
这将:
20110508223913_create_states 的迁移.rb
,它看起来与您上面尝试的迁移几乎相同。然后您需要做的就是添加您的
State.create...
行,然后就可以开始了。Your code would work perfectly, except that you don't actually have a
State
class. The only way Rails would know about this class is if you defined it inapp/models/state.rb
asClass State < ActiveRecord::Base...
Rather than running a custom migration, I'd recommend running this line of code:
This will:
20110508223913_create_states.rb
, which will look nearly identical to your attempted migration above.Then all you need to do is add your
State.create...
lines and you should be good to go.