Ruby-on-rails 如何在 Rails 迁移中添加一些插入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2667580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add some inserts in rails migration?
提问by xaver23
After creating a table (by migration), I want to insert some entries directly. How must I write a migration for this?
创建表(通过迁移)后,我想直接插入一些条目。我必须如何为此编写迁移?
thanks
谢谢
采纳答案by Ju Nogueira
Update:This is the right answer: https://stackoverflow.com/a/2667747/7852
更新:这是正确的答案:https: //stackoverflow.com/a/2667747/7852
Here's an example from ruby on rails api:
这是ruby on rails api的一个例子:
class AddSystemSettings < ActiveRecord::Migration
# create the table
def self.up
create_table :system_settings do |t|
t.string :name
t.string :label
t.text :value
t.string :type
t.integer :position
end
# populate the table
SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1
end
def self.down
drop_table :system_settings
end
end
回答by Ryan McGeary
Don't. If you're looking for seed data, you should use db/seeds.rband rake db:seedinstead. More info in this Railscast.
不要。如果您正在寻找种子数据,您应该使用db/seeds.rbandrake db:seed代替。 此 Railscast 中的更多信息。
Side note: Always make sure that the code in db/seeds.rbis idempotent. i.e. It should always be safe to re-run your seeds.
旁注:始终确保代码db/seeds.rb是幂等的。即重新运行您的种子应该总是安全的。
But, if you must insert or modify data inside a migration (there are legitimate use-cases for this), it's best to use SQL statements instead. Your model class isn't guaranteed to still be around in the same form in a future version of your application, and running the migrations from scratch in the future might yield errors if you reference the model class directly.
但是,如果您必须在迁移中插入或修改数据(有合法的用例),最好改用 SQL 语句。您的模型类不能保证在您的应用程序的未来版本中仍然以相同的形式存在,并且如果您直接引用模型类,将来从头开始运行迁移可能会产生错误。
execute "insert into system_settings (name, label, value) values ('notice', 'Use notice?', 1)"
回答by David Mayo
Edit: PLEASE NOTE - Posters above are correct, you should not populate the DB inside migrations. Don't use this to add new data, only to modify data as part of changing the schema.
编辑:请注意 - 上面的海报是正确的,你不应该在迁移中填充数据库。不要使用它来添加新数据,仅用于修改数据作为更改架构的一部分。
For many things, using raw SQL will be preferable, but if you need to insert data as part of a migration (for instance, doing data conversion when breaking out a table into multiple tables), and you want some default AR stuff like convenient DB-independent escaping, you can define a local version of the model class:
对于很多事情,使用原始 SQL 会更可取,但是如果您需要将数据作为迁移的一部分插入(例如,在将表分解为多个表时进行数据转换),并且您想要一些默认的 AR 东西,例如方便的 DB -independent 转义,可以定义模型类的本地版本:
class MyMigrationSucksALittle < ActiveRecord::Migration
class MyModel < ActiveRecord::Base
# empty guard class, guaranteed to have basic AR behavior
end
### My Migration Stuff Here
### ...
end
Note that this works best for simple cases; since the new class is in a different namespace (MyMigrationSucksALittle::MyModel), polymorphic associations declared in the guard model won't work correctly.
请注意,这最适用于简单的情况;由于新类位于不同的命名空间 ( MyMigrationSucksALittle::MyModel) 中,因此在保护模型中声明的多态关联将无法正常工作。
A somewhat more detailed overview of available options is located here: http://railsguides.net/2014/01/30/change-data-in-migrations-like-a-boss/
可用选项的更详细概述位于此处:http: //railsguides.net/2014/01/30/change-data-in-migrations-like-a-boss/
回答by Salil
create a new migration file like 047_add_rows_in_system_settings.rb
创建一个新的迁移文件,如 047_add_rows_in_system_settings.rb
class AddRowsInAddSystemSettings < ActiveRecord::Migration
def self.up
SystemSetting.create{:name => "name1", :label => "Use notice?", :value => 1}
SystemSetting.create{:name => "name2", :label => "Use notice?", :value => 2}
end
def self.down
SystemSetting.delete_all
end
end
OR
或者
while creating table
创建表时
046_system_settings.rb
046_system_settings.rb
class AddSystemSettings < ActiveRecord::Migration
def self.up
create_table :system_settings do |t|
t.string :name
t.string :label
t.text :value
t.string :type
t.integer :position
end
SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1
end
def self.down
drop_table :system_settings
end
end
Ref:- http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
参考:- http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
回答by Jeremie Ges
Use can use seed data, it's a great way for this ! http://railscasts.com/episodes/179-seed-data
使用可以使用种子数据,这是一个很好的方法!http://railscasts.com/episodes/179-seed-data

