我想在数据库中创建一个新表,而不是在database.yml文件中定义的表。
这是我的database.yml文件:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_development
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_test
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_prod
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
我有另一个名为“peripheral”的数据库。我想在该数据库中创建一个名为“retailer_to_domain”的表。
这是我的迁移文件:
class CreateRetailerToDomains < ActiveRecord::Migration
def connection
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:encoding => "utf8",
:reconnect => false,
:database => "peripheral",
:pool => 5,
:username => "root",
:password => "",
:socket => "/var/run/mysqld/mysqld.sock").connection
end
def change
ActiveRecord::Base.connection.create_table :retailer_to_domains do |t|
t.string :name
t.string :domain
t.timestamps
end
end
end
迁移文件是由命令rails generate model RetailerToDomain name:string domain:string
生成的,然后我添加了def connection
方法来覆盖database.yml配置中的默认数据库(“main”)。
当我运行迁移(rake db:migrate
)时,将在main_development数据库中创建retailer_to_domains表。如何覆盖此默认值以获取迁移以创建我想要的表?
另外,我希望RetailerToDomain模型以类似的方式访问此表,并使用如下所示的establish_connection方法:
class RetailerToDomain < ActiveRecord::Base
establish_connection(
:adapter => "mysql2",
:encoding => "utf8",
:reconnect => false,
:database => "peripheral",
:pool => 5,
:username => "root",
:password => "",
:socket => "/var/run/mysqld/mysqld.sock")
self.table=retailer_to_domain
validates_presence_of :name, :domain
end
提前感谢您的任何想法!
答案 0 :(得分:4)
观看此页面!
https://github.com/rails/rails/issues/3497
创建rake文件
# Augment the main migration to migrate your engine, too.
task 'db:migrate', 'your_engine:db:migrate'
# Augment to dump/load the engine schema, too
task 'db:schema:dump', 'your_engine:db:schema:dump'
task 'db:schema:load', 'your_engine:db:schema:load'
namespace :your_engine do
namespace :db do
desc 'Migrates the your_engine database'
task :migrate => :environment do
p "your_engine db migrate"
with_engine_connection do
ActiveRecord::Migrator.migrate("#{File.dirname(__FILE__)}/../../db/migrate/your_engine", ENV['VERSION'].try(:to_i))
end
Rake::Task['your_engine:db:schema:dump'].invoke
end
task :'schema:dump' => :environment do
require 'active_record/schema_dumper'
with_engine_connection do
File.open(File.expand_path('../../../db/your_engine_schema.rb', __FILE__), 'w') do |file|
ActiveRecord::SchemaDumper.dump ActiveRecord::Base.connection, file
end
end
end
task :'schema:load' => :environment do
with_engine_connection do
load File.expand_path('../../../db/your_engine_schema.rb', __FILE__)
end
end
end
end
# Hack to temporarily connect AR::Base to your_engine.
def with_engine_connection
original = ActiveRecord::Base.remove_connection
ActiveRecord::Base.establish_connection "your_engine_#{Rails.env}".to_sym
yield
ensure
ActiveRecord::Base.establish_connection original
end
模型
class YourModel < ActiveRecord::Base
establish_connection "your_engine_#{Rails.env}".tom_sym
end
database.yaml
your_engine_development:
adapter:
database:
encoding:
username:
password:
host:
答案 1 :(得分:0)
Add another database connection seeting to database.yml file.
database.yml file
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_development
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_test
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_prod
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
another_database:
adapter: "mysql2",
encoding: "utf8",
reconnect: false,
database: "peripheral",
pool: 5,
username: "root",
password: "",
socket: "/var/run/mysqld/mysqld.sock"
Now In Migration file specify:
ActiveRecord::Base.establish_connection(:another_database)
class CreateRetailerToDomains < ActiveRecord::Migration
ActiveRecord::Base.establish_connection(:another_database)
def change
create_table :retailer_to_domains do |t|
t.string :name
t.string :domain
t.timestamps
end
end
end
It will get create table inside that database only.
Model
class RetailerToDomain < ActiveRecord::Base
ActiveRecord::Base.establish_connection(:another_database)
end
**To check the active database connection**
ActiveRecord::Base.connection.current_database
答案 2 :(得分:0)
在config/database.yml
:
new_connect_development:
adapter: mysql2
encoding: utf8
reconnect: false
database: new_database_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
与所有其他环境相同的块。然后添加到您的迁移(示例for rails 2.x-3.0,for rails 3.1 remove self
):
def self.connection
ActiveRecord::Base.establish_connection("new_connect_#{RAILS_ENV}").connection
end
并在模型中添加以下行,与新数据库中的表对应:
class ModelName < ActiveRecord::Base
establish_connection "new_connect_#{RAILS_ENV}"
end