问题
I'm using active_delegate for multiple connection in Rails. Here I'm using mysql as master_database for some models,and postgresql for some other models.
Problem is that when I try to access the mysql models, I'm getting the error below! Stack trace shows that, it is still using the postgresql adapter to access my mysql models!
RuntimeError: ERROR C42P01 Mrelation "categories" does not exist P15 F.\src\backend\parser\parse_relation.c L886 RparserOpenTable: SELECT * FROM "categories"
STACKTRACE
===========
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:507:in `execute'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:985:in `select_raw'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:972:in `select'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:81:in `cache_sql'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:661:in `find_by_sql'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1553:in `find_every'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:615:in `find'
D:/ROR/Aptana/dedomenon/app/models/category.rb:50:in `get_all_with_exclusive_scope'
D:/ROR/Aptana/dedomenon/app/models/category.rb:50:in `get_all_with_exclusive_scope'
D:/ROR/Aptana/dedomenon/app/controllers/categories_controller.rb:48:in `index'
here is my database.yml
file
postgre: &postgre
adapter: postgresql
database: codex
host: localhost
username: postgres
password: root
port: 5432
mysql: &mysql
adapter: mysql
database: project
host: localhost
username: root
password: root
port: 3306
development:
<<: *postgre
test:
<<: *postgre
production:
<<: *postgre
master_database:
<<: *mysql
and my master_database
model is like this
class Category < ActiveRecord::Base
delegates_connection_to :master_database, :on => [:create, :save, :destroy]
end
Anyone has any solution??
回答1:
Another way:
class Abc < ActiveRecord::Base
establish_connection Rails.configuration.database_configuration["test"]
end
回答2:
might want to check out https://github.com/tchandy/octopus nowadays.
回答3:
This will change the database connection for a single model object.
$config = YAML.load_file(File.join(File.dirname(__FILE__),
'../config/database.yml'))
class ModelWithDifferentConnection < ActiveRecord::Base
establish_connection $config['connection_name_from_database_yml']
end
If you are using the same server but just a different database file then you can do something like this instead.
class ModelWithDifferentConnection < ActiveRecord::Base
# Lives in the CURRICULUM database
def self.table_name
"database.table"
end
end
回答4:
I highly suggest MyReplication plugin for MySQL adapter which helps you switch the connection at run-time in an elegant way:
User.using(:another_database) do
u = User.all
end
https://github.com/minhnghivn/my_replication
回答5:
I tried ur Sample,still getting error!!
superclass mismatch for class MysqlAdapter
I think ,the problem is with my database.yml
file .Please check this file
database_mysql:
adapter: mysql
database: project
host: localhost
username: root
password: root
port: 3306
development:
adapter: postgresql
database: codex
host: localhost
username: postgres
password: root
port: 5432
test:
adapter: postgresql
database: codex
host: localhost
username: postgres
password: root
port: 5432
production:
adapter: postgresql
database: codex
host: localhost
username: postgres
password: root
port: 5432
i start the mongrel in developemnet mode only.
here is my model superclass
$config = YAML.load_file(File.join(File.dirname(__FILE__),
'../../config/database.yml'))
class MasterDatabase < ActiveRecord::Base
self.abstract_class = true
establish_connection $config['database_mysql']
end
Please correct me..
回答6:
I've also had to connect to, and manage, two different databases, so I created a gem called secondbase: http://github.com/karledurante/secondbase
回答7:
I don't know about active_delegate, but I recently had to access different databases for work applications, and nothing really fit what I wanted. So I wrote something for myself, it's running in production applications as we speak.
Fixed Link connection_ninja
来源:https://stackoverflow.com/questions/1298909/multiple-database-connection-in-rails