dora
dora

Reputation: 1344

Mysql2 and Sqlite3 in Rails

I have a rails application which uses two different types of databases : sqlite3 and mysql2.

I did some searching and managed to get this far, my database.yml file looks like this :

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000


mysql2_development:
  adapter: mysql2
  database: db_name
  username: user_name
  password: pass_word
  host: somehost.net
  port: 3306
  socket: /tmp/mysql.sock

I created the following classes in models to access my Mysql2 database:

class NewDB < ActiveRecord::Base

  # To change this template use File | Settings | File Templates.
  self.abstract_class = true
  establish_connection "mysql2_#{Rails.env}"
end

class PROJECT < NewDB
end

Here PROJECT is table present in the "NewDB" database.

Problem:

Is this the right way to do it , Also how do I access them from the controller ?

In my controller I tried this:

class NewDBController < ApplicationController

  def index
    @projects=PROJECT.all
  end
end

But I get the following error "Mysql2::Error: Table 'newdb.projects' doesn't exist: SHOW FULL FIELDS FROM projects"

Upvotes: 2

Views: 333

Answers (1)

Simon Woker
Simon Woker

Reputation: 5034

Did you run a migration for the other environment?

rake db:migrate RAILS_ENV=mysql2_development

Also your Project class should be called Project instead. You can use:

class Project < NewDB
  set_table_name "PROJECT"
end

Upvotes: 2

Related Questions