Building rails app on existing Postgres Database

15 views
Skip to first unread message

Ben Edwards

unread,
Sep 1, 2017, 9:56:35 AM9/1/17
to Ruby on Rails: Talk
Hi, I have a fairly simple database with a few tables and want to build a rails app on it.  I know I need to do some changes to make it conform to ActiveRecord conventions but when I have done that how do I best access it from rails?  I have found this, https://codeburst.io/how-to-build-a-rails-app-on-top-of-an-existing-database-baa3fe6384a0 whitch looks usefull (and done a load of googleing) but figured this must be something that people have done before and documenter in a guide/howto.  Can someone point me in the wright direction?

Hassan Schroeder

unread,
Sep 1, 2017, 10:37:24 AM9/1/17
to rubyonrails-talk
On Fri, Sep 1, 2017 at 6:56 AM, Ben Edwards <lo...@funkytwig.com> wrote:
> Hi, I have a fairly simple database with a few tables and want to build a
> rails app on it. I know I need to do some changes to make it conform to
> ActiveRecord conventions but when I have done that how do I best access it
> from rails?

Really, all you need to do is generate a new rails app with postgres
specified as the database and configure it to use the existing DB
(presumably the copy you've modified to be more AR-compliant).

That "schema_to_scaffold" gem *might* be useful -- it looks pretty
old so hard to tell without trying it -- but if you have "a few" tables
then creating AR Models from them manually won't be a lot of work.

Do make use of `git` to save your work as you go -- you'll probably
wind up throwing away some experiments :-)

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Ben Edwards

unread,
Sep 1, 2017, 11:25:21 AM9/1/17
to Ruby on Rails: Talk
So I basicaly recreate the tables from scratch manualy, using rails to create them.  I was worried importing the data may be a bit fiddely if I did this (quess I can force rails to use specific data types).  Was hoping there was a way rails could help with this.  It may be only a few tables but there are quite a few columns.

Hassan Schroeder

unread,
Sep 1, 2017, 11:53:04 AM9/1/17
to rubyonrails-talk
On Fri, Sep 1, 2017 at 8:25 AM, Ben Edwards <lo...@funkytwig.com> wrote:
> So I basicaly recreate the tables from scratch manualy, using rails to
> create them.

NO! *IF* you have columns that don't adhere to AR conventions
you can rename them manually but that's trivial and won't affect
your existing data.

Seriously, don't overthink this -- just create the app, point it at your
DB copy and try it.

Let's say you have a table "things"; open a console and enter

class Thing < ApplicationRecord
end

That is literally all you need to start. Then "Thing.first" will show
you a record with all the attributes of a "thing".

Ben Edwards

unread,
Sep 1, 2017, 12:08:12 PM9/1/17
to Ruby on Rails: Talk
Great, so youn dont need to give rails a model file?

Hassan Schroeder

unread,
Sep 1, 2017, 12:18:25 PM9/1/17
to rubyonrails-talk
On Fri, Sep 1, 2017 at 9:08 AM, Ben Edwards <lo...@funkytwig.com> wrote:
> Great, so youn dont need to give rails a model file?

You will have to create a model file (and controller, and views), yes,
but for exploratory/testing purposes you can define a class (model)
right in the console and it will work.

Colin Law

unread,
Sep 1, 2017, 12:20:55 PM9/1/17
to Ruby on Rails: Talk
On 1 September 2017 at 17:08, Ben Edwards <lo...@funkytwig.com> wrote:
> Great, so youn dont need to give rails a model file?

I suggest you start by working right through a good tutorial such as
railstutorial.org, which is free to use online. That will show you the
basics of Rails and will save you a lot of time in the long run.

Colin
Reply all
Reply to author
Forward
0 new messages