views:

2701

answers:

4

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_databse model is like this

class Category < ActiveRecord::Base

  delegates_connection_to :master_database, :on => [:create, :save, :destroy]

end

Anyone has any solution??

A: 

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

railsninja
That page doesn't exist!
Nicolas Buduroi
A: 

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
Ken
A: 

Hi Ken,
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..