How do I connect to an existing Rails database?

Asked

Viewed 1,942 times

0

I wonder if there is how to connect to Postgresql, so I only read the data of the tables that are already created.

I am developing an API, which will read in a given database, and return an XML, for example.

1 answer

3


Yes, Rails was made to support legacy databases as well, only you will have to make additional settings.

First, configure the file config/database.yml with your bank settings.

When generating models and scaffolding, use the option --no-migration to avoid the creation of tables (which already exist):

rails g model post title text:text --no-migration
rails g scaffold post title text:text --no-migration

Then manually configure the table name and primary key:

class Product < ActiveRecord::Base
  self.table_name = "PRODUCT"
  self.primary_key = "product_id"
end

If using TDD, also configure the fixtures file in the model’s Testcase:

class FunnyJoke < ActiveSupport::TestCase
  set_fixture_class funny_jokes: Joke
  fixtures :funny_jokes
  ...
end

Remember, the Rails guide is your friend: http://guides.rubyonrails.org/

Source: http://guides.rubyonrails.org/active_record_basics.html#Overriding-the-naming-Conventions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.