可以ActiveRecord的创建迁移之外的表吗? [英] Can ActiveRecord create tables outside of a migration?

查看:160
本文介绍了可以ActiveRecord的创建迁移之外的表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在非Rails的Web应用程序,所以没有迁移脚本,默认情况下。

I am working on a non Rails web app, so no migrations script by default.

续集ORM让我在脚本中轻松地创建表:

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

有没有办法待办事项这与ActiveRecord的迁移之外?

Is there a way todo this with ActiveRecord outside of migrations?

推荐答案

我目前的理解是没有,所有的修改数据或模式必须通过迁移来完成。我已经在GitHub上 一个完整的Rake文件从而可以用于导轨之外进行迁移。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆