我们最近修改了一个项目,并且正在将所有旧的数据带入新系统.问题是模式略有不同,所以直接的sql导入是不可能的.由于某些非规范化和数据库更改,我们需要在准备好导入之前对数据进行按摩.我希望像这样的东西:
OldUser.all.each do |ou| NewUser.create({ :first_name => ou.first_name :last_name => ou.last_name :login => ou.login :company_name => ou.company.name }) end
在上面的示例中,OldUser正在从旧数据库读取,NewUser正在处理新的数据库.我需要两组模型(新的和旧的)来保留它们的关联,以正确地对某些数据进行非规范化.
有没有项目/图书馆可以帮助我做到这一点?
解决方法@H_404_10@
您应该直接在模型或database.yml中指定连接选项.我们最初去第一条路线:
# This is the new users table - connects to development/test/production
# DB from database.yml
class User < ActiveRecord::Base
end
class OldUser < ActiveRecord::Base
establish_connection :adapter => "postgresql",:database => "legacy_users",:username => "whatever",:password => "something"
set_table_name "u_users" # Whatever you require
belongs_to :company,:class_name => "OldCompany",:foreign_key => "fk_company_id"
end
class OldCompany < ActiveRecord::Base
establish_connection :adapter => "postgresql",:password => "something"
set_table_name "u_company" # Whatever you require
has_many :users,:class_name => "OldUser",:foreign_key => "fk_company_id"
end
从常规代码中,您可以使用以下模型:
OldUser.find_each do |ouser|
User.create!(:username => ouser.username,:company_name => ouser.company.name)
end
ActiveRecord将为您处理所有的细节.
现在,如果你像我一样,你不喜欢在你的模型中添加这样的细节级别 – 用户名,密码等.简单的 – 将配置移动到database.yml并使用正确的建立连接语法进行连接:
# database.yml
development:
adapter: postgresql
# go on as usual,for all 3 envs
legacy_users_development:
adapter: postgresql
database: legacy_users
username: whatever
password: something
注意命名约定 – legacy_users _#{Rails.env}是我在这里的目标,这里是如何做的:
class OldUser < ActiveRecord::Base
establish_connection "legacy_users_#{Rails.env}"
set_table_name "u_users" # Whatever you require
belongs_to :company,:foreign_key => "fk_company_id"
end
宾果,其他一切都会奏效.
# This is the new users table - connects to development/test/production # DB from database.yml class User < ActiveRecord::Base end class OldUser < ActiveRecord::Base establish_connection :adapter => "postgresql",:database => "legacy_users",:username => "whatever",:password => "something" set_table_name "u_users" # Whatever you require belongs_to :company,:class_name => "OldCompany",:foreign_key => "fk_company_id" end class OldCompany < ActiveRecord::Base establish_connection :adapter => "postgresql",:password => "something" set_table_name "u_company" # Whatever you require has_many :users,:class_name => "OldUser",:foreign_key => "fk_company_id" end
从常规代码中,您可以使用以下模型:
OldUser.find_each do |ouser| User.create!(:username => ouser.username,:company_name => ouser.company.name) end
ActiveRecord将为您处理所有的细节.
现在,如果你像我一样,你不喜欢在你的模型中添加这样的细节级别 – 用户名,密码等.简单的 – 将配置移动到database.yml并使用正确的建立连接语法进行连接:
# database.yml development: adapter: postgresql # go on as usual,for all 3 envs legacy_users_development: adapter: postgresql database: legacy_users username: whatever password: something
注意命名约定 – legacy_users _#{Rails.env}是我在这里的目标,这里是如何做的:
class OldUser < ActiveRecord::Base establish_connection "legacy_users_#{Rails.env}" set_table_name "u_users" # Whatever you require belongs_to :company,:foreign_key => "fk_company_id" end
宾果,其他一切都会奏效.