How do i work with two different databases in rails with active records?

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

I need to use different database connections in different Rails models. Is there a not-so-hacky way to do that?

Any links or search keywords would be great :)

回答1:

mikej is right. I did however write a gem that makes the model code to connect a little bit cleaner, check it out.



回答2:

Add new sections to your database.yml e.g.

other_development:   adapter: mysql   database: otherdb_development   username: root   password:   host: localhost  other_production:   adapter: mysql   database: otherdb_production   username: root   password:   host: localhost

Add a class in lib/other_database.rb

class OtherDatabase < ActiveRecord::Base   establish_connection "other_#{RAILS_ENV}" end

and then for each model which isn't in the default database subclass from OtherDatabase e.g.:

class MyModel < OtherDatabase    # my model code... end


回答3:

I have been using the following to connect to 2 db in the same app. I put them in lib folder since everything in there is loaded.

require 'active_record'  class OldDatabase < ActiveRecord::Base   self.abstract_class = true   establish_connection(   :adapter  => 'mysql',   :database => 'weather',   :host     => 'localhost',   :username => 'root',   :password => 'password'   ) end  class NewDatabase < ActiveRecord::Base   self.abstract_class = true   establish_connection(   :adapter  => 'mysql',   :database => 'redmine',   :host     => 'localhost',   :username => 'root',   :password => 'password'   ) end  class WeatherData < OldDatabase end  class Board < NewDatabase end

Hope that helps



回答4:

Update for Rails 3.x:

class MyModel < ActiveRecord::Base   establish_connection "other_#{Rails.env}" end


回答5:

I think that the prettiest way to connect to another database with active model is creating base class for external database, and then inherite from that base in your model. This method works fine with rails 4.2.6 and 5.0.4

For example:

# in /models/external_db/base.rb require 'active_record'  class ExternalDb::Base < ActiveRecord::Base   self.abstract_class = true   establish_connection "external_db_#{Rails.env}".to_sym end

And in your model class:

# in /models/external_db/some_model.rb class ExternalDB::SomeModel < ExternalDb::Base   # your code end

But you must define external database in /config/database.yml

# in /config/database.yml external_db_development:   adapter: sqlite3   pool: 5   timeout: 5000   database: db/external_db_development.db  external_db_production:   adapter: sqlite3   pool: 5   timeout: 5000   database: db/external_db_production.db


回答6:

In rails 4.1+ establish_connection now takes a symbol:

class OtherDbModel < ActiveRecord::Base   establish_connection :"other_#{Rails.env}" end


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!