How to Use Existing Database with Rails

How to Use Existing Database with Rails

19 Jan 2015

tags: rails ruby database

So I have this Rails project that I am working on with a friend. We got started right before winter break started; I was building the backend in while she was working on the frontend, but since neither of us were very well-versed in webdev, we dabbled in a little of the other side as well.

By the end of the break, I had all the structure done in Rails and a local sqlite database while she started a postgresql database on AWS and started putting materials into it. It took me hours to figure out how to do these, so I wanted to document them.

Converting from Sqlite to Postgres

After installing postgres and pgAdmin on my computer, I deleted all instances of gem 'sqlite3' in my file to gem pg, '1.7.0. Then, I ran bundle install in the terminal.

Then, in the /config/database.yml file, I changed the yaml file to include info from the postgres db I am trying to add.

<pre><code>development:
  adapter: postgresql
  encoding: unicode
  database: &lt;DATABASE_NAME&gt;
  pool: 5
  username: &lt;USERNAME&gt;
  password: &lt;PASSWORD&gt;
  host: &lt;either LOCALHOST or AWS port address&gt;
  port: 5432 &lt;or whichever your port is under&gt;
</code></pre>

Now, to see if I connected to the database or not, I ran bundle install and rake db:migrate:reset. My model originally had an existing User model which generated a users table in the sqlite3 database, and it was added to the postgres db too after I ran migrate. When I checked the file /db/schema.rb, I could see all the models and the parameters that already existed in the postgres database. I wasn't sure if the last step was necessary, but I ran rake db:migrate:schema to make sure that the migration is complete.

Accessing Parameter Values from Non-Default Postgres DB

Now that I had changed the database for my app from sqlite3 to postgres, I moved on to figuring out how to access tables inside the database. While my User model is still connected to the users table in the postgres db, I created an empty model to connect to a table in the db.
rails generate model Material

And, in the model file /app/models/material.rb, I added a few things after digging around on StackOverflow and code blogs and this is what worked for me.

<pre><code class="language-ruby">class Material &lt; ActiveRecord::Base 
    self.abstract_class = true           #needed after Rails3 
    establish_connection &quot;#{Rails.env}&quot;     #database name 
    self.table_name = &quot;lessons_lesson&quot; 
end
</code></pre>

Since I didn't create the table, so most of the table names didn't follow Rails convention of making table name plural of the model name, but that could be overridden by declaring the table name with self.table_name = "<TABLE_NAME>"

To access values in the table, I created a controller, rails generate controller Materials, and I simply wanted to see if I could access the first item in the lessons_lesson table in the db, so this was my /app/controllers/materials_controller.rb:

<pre><code class="language-ruby">class MaterialsController &lt; ApplicationController
    def show
        @material = Material.first
    end
end
</code></pre>

After updating my /config/routes.rb to include the controller I just created recources :materials, I was good to go to generate a simple page that showed the parameter value in the first object in the table 'lessons_lesson' in /app/views/materials/show.html.erb:

<pre><code>&lt;h1&gt;Lesson Title&lt;/h1&gt;
&lt;%= @material.title %&gt;
</code></pre>