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?
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
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.
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
That is literally all you need to start. Then "Thing.first" will show
you a record with all the attributes of a "thing".
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.
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.