【发布时间】:2013-07-02 16:41:04
【问题描述】:
我正在尝试使用 establish_connection 迁移到 Rails 中的单独数据库。我在迁移文件中有这样的内容:
def change
ActiveRecord::Base.establish_connection("user_#{Rails.env}")
ActiveRecord::Base.initialize_schema_migrations_table
create_table "users", :force => true do |t|
t.string "email",
end
在 yml 我有这个配置:
user_development:
adapter: postgresql
encoding: unicode
database: MyApp_user_development
pool: 5
host: localhost
username: xxx
password: xxx
我还定义了这样的用户模型:
class User < ActiveRecord::Base
establish_connection "user_#{Rails.env}"
[...]
end
现在,数据库存在,但在运行 rake db:migrate 时出现此错误:
== CreateUserOnSeparateDb: migrating =========================================
rake aborted!
An error has occurred, this and all later migrations canceled:
connection is closed
你们中有人知道发生了什么吗?
我还尝试将establish_connection 调用移动到迁移文件中的connection 方法中。在这种情况下,该表是在迁移时创建的,但随后我得到了同样的错误(因此在其他迁移中以某种方式失败)并且顺便说一下它没有创建 schema_migrations 表......
有什么帮助吗?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 rails-migrations