2012-07-23 39 views
0

我有一個使用DataMapper的Rails應用程序。我即將將其轉換爲使用ActiveRecord。如果可能的話,我一次只想做一個模型(或一組模型)。我可以讓DataMapper和ActiveRecord在Rails應用程序中使用不同的數據庫適配器嗎?

現在,我甚至無法啓動應用程序,因爲兩個ORM似乎需要不同的適配器。

如果database.yml指定適配器mysqlbundler/rubygems_integration.rb提出:

Please install the mysql adapter: `gem install activerecord-mysql-adapter` (mysql is not part of the bundle. Add it to Gemfile.) 

如果指定mysql2activesupport/dependencies提出:

cannot load such file -- dm-mysql2-adapter (LoadError) 

我試圖與mysql2適配器創建ActiveRecord的一個單獨的環境,然後使用我想先轉換的個人模型中的establish_connection,但該應用程序仍然無法啓動。

有沒有人做過這個成功?

+0

更新:我從來沒有得到他們運行並排側;我剛剛做了一個大規模的替換,其中一切都被打破了一段時間。 :(祝你好運,任何人試圖這樣做。 – 2012-12-13 13:36:51

回答

0

此問題的解決方案似乎是將DataMapper的數據庫連接信息作爲uri選項,而ActiveRecord將使用由適配器,用戶名,root和主機選項指定的連接。

defaults: &defaults 
    adapter: mysql2 
    username: root 
    password: somePassword 
    host: localhost 

development: &development 
    database: myapp_dev 
    uri: mysql://localhost/myapp_dev?user=root&password=somePassword 
    <<: *defaults 

test: 
    database: myapp_test 
    uri: mysql://localhost/myapp_test?user=root&password=somePassword 
    <<: *defaults 
production: 
    database: myapp_dev 
    <<: *defaults 

雖然讓兩個ORM很好地一起玩會涉及很多其他問題。就我個人而言,我已經放棄了,並且只是將DataMapper徹底解封。

0

你可以試試這個。這個代碼需要在你的ActiveRecord模型被加載後運行。

db_settings = YAML::load_file("#{ROOT_PATH}/config/database_settings_for_active_record.yml") 
db_config = db_settings[Rails.env] 
spec = Hash.new 
[:adapter, :host, :username, :password, :pool, :database].each { |x| spec.merge!(x => db_config[x.to_s]) } 
YourModel.establish_connection(spec) 

database_settings_for_active_record.yml將包含這樣的內容:

production: 
    adapter: mysql2 
    encoding: utf8 
    host: localhost 
    username: root 
    password: 
    pool: 5 
    database: my_db 
相關問題