ActiveRecord可以在迁移之外创建表吗? [英] Can ActiveRecord create tables outside of a migration?
本文介绍了ActiveRecord可以在迁移之外创建表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用非Rails网络应用程序,因此默认情况下没有迁移脚本。
Sequel ORM让我可以在脚本中轻松创建表格:
#!/ usr / bin / env ruby
需要'rubygems'
require'sequel'
##连接到数据库
DB = Sequel.sqlite('./ ex1.db')
除非DB.table_exists? :posts
DB.create_table:posts do
primary_key:id
varchar:title
text:body
end
end
$有没有办法使用ActiveRecord在迁移之外?
<解决方案我目前的理解是,所有的修改数据或模式都必须通过迁移来完成。我有 github上的完整rakefile
或者,如果它只是一个初始化脚本,可以使用以下脚本。
ActiveRecord :: Base.establish_connection(
:adapter =>'sqlite3',
:database =>'./lesson1_AR.db '
)
ActiveRecord :: Migration.class_eval do
create_table:posts do | t |
t.string:title
t.text:body
end
create_table:people do | t |
t.string:first_name
t.string:last_name
t.string:short_name
end
create_table:tags do | t |
t.string:tags
end
end
I am working on a non Rails web app, so no migrations script by default.
The Sequel ORM lets me create tables easily in a script:
#!/usr/bin/env ruby
require 'rubygems'
require 'sequel'
## Connect to the database
DB = Sequel.sqlite('./ex1.db')
unless DB.table_exists? :posts
DB.create_table :posts do
primary_key :id
varchar :title
text :body
end
end
Is there a way todo this with ActiveRecord outside of migrations?
解决方案 My current understanding is no, all modifications data or schema have to be done through a migration. I have a complete rakefile on github which can be used to perform the migrations outside of Rails.
Alternatively if it is just an initialisation script the following could be used.
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => './lesson1_AR.db'
)
ActiveRecord::Migration.class_eval do
create_table :posts do |t|
t.string :title
t.text :body
end
create_table :people do |t|
t.string :first_name
t.string :last_name
t.string :short_name
end
create_table :tags do |t|
t.string :tags
end
end
这篇关于ActiveRecord可以在迁移之外创建表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文