Search code examples
mysqlruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1

Working with existing database of a rails project


I want to run a ruby on rails project in my Ubuntu 12 mechine.After running the command bundle install i used the following command

   rake db:drop db:setup 

it shows

-- create_table("art_clas", {:force=>true})
   -> 0.3222s
-- add_index("art_clas, ["art_id", "clas, {:name=>"index_art_attes_on_art_attribute", :unique=>true})
   -> 0.4328s
.....................................
.....................................

while i using $ rake db:setup command it shows the existing db's

franche_development already exists
franche_test already exists
 -- create_table("art_clas", {:force=>true})
       -> 0.3222s
    -- add_index("art_clas, ["art_id", "clas, {:name=>"index_art_attes_on_art_attribute", :unique=>true})
       -> 0.4328s
    .....................................

Later when i run the project it doesn't show any value from database tables(showing blank). after that i typed following to show the databases. But this operation does not shows my existing databases in the rails project

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.23 sec)

Executing rake db:drop returns nothing

 $ rake db:drop
 $

and when running the project displaying

Mysql2::Error: Table 'franche_development.arts' doesn't exist: SELECT `arts`.* FROM `arts`  WHERE `arts`.`active` = 1 .....

How can i set up and use the existing databases and tables from rails project.

Is it due to any code error or database setup??


Solution

  • So you want to use an existing database created in mySQL in ruby. You need to setup an configuration in config/database.yml.

    here is an example

    development:
    adapter: mysql2
    encoding: utf8
    database: my_db_name
    username: root
    password: my_password
    host: 127.0.0.1
    port: 3306

    Also you need to install mysql gem. and also update your Gemfile about mysql gem

    By default rails setup databases in sqlite and not in mySQL and is stored in RAILS_ROOT/db/development.sqlite3 . This means that you will not be able to see the tables by using mysql if you have created them in rails, you need to use sqlite3 in order to interact with them