我正在处理一个现有的 Rails 项目,我必须在其中添加一个新选项卡。好吧,我已经编写了所有相应的模型、 View 和 Controller ,然后更改了 schema.rb 文件以创建新表。
create_table "ryan_indices", :force => true do |t|
t.string "name"
end
create_table "benchmark_indices", :force => true do |t|
t.string "name"
end
这是我的部分 schema.rb 文件。第一张 table 以前在那里。我只是想添加一个类似的新表,我为其创建了模型、 View 和 Controller ,还在 schema.rb 文件中添加了那些 create_table 语句。但是,当我运行 rake db:migrate
时,它不会创建新表。 schema.rb 文件回到以前的状态
我的意思是在我运行 rake db:migrate
命令后我的更改消失了,它在命令行屏幕上没有显示任何内容。
我不知道我错过了什么。有人可以帮忙吗?
最佳答案
编辑schema.rb is considered to be bad practice :
...not designed to be edited, they just represent the current state of the database.
要创建新模型(以及新的数据库表),请调用 Rails 模型生成器
rails generate model Product name:string
或者:对于现有模型的新字段,请 generate a migration ...
rails generate migration AddNameToProducts
...编辑它然后用“rake db:migrate
”运行它
这些任务在官方 RailsGuides > Migrations 中有很好的描述
关于ruby-on-rails - rails : rake db:create is not creating the new table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14225448/