Helpful Information
 
 
Category: Ruby Programming
Using existing database with Ruby on Rails

Hi guys I'm on the early stages on learning Ruby. I got a good collection of ebooks that I'm reading myself on my free time.

The thing is that I really don't have an idea on how to use an existing database filled with tables and data on ruby. Every guide, every article that I have or find on the internet is always creating a new one using the migration functions.

But which are the steps for using an existing database on RoR?


Thanks in advance.

The procedure is very simple.
1. Create a new rails project.
2. Edit your_project_name/config/database.yml and set up the connection parameters so that it connects to your existing database.
3. Type the following for each table in your existing database:


script/generate model tablename

and substitute tablename for each table in your database. This will create ruby objects to access the existing tables in your database.

As you can see, there's nothing complicated about the procedure.

The procedure is very simple.
1. Create a new rails project.
2. Edit your_project_name/config/database.yml and set up the connection parameters so that it connects to your existing database.
3. Type the following for each table in your existing database:


script/generate model tablename

and substitute tablename for each table in your database. This will create ruby objects to access the existing tables in your database.

As you can see, there's nothing complicated about the procedure.

Awesome thanks !!!

Also, to take advantage of rails' built in features, read *UP* on ActiveRecord. There are a tremendous number of conventions you might want to take advantage of. One example, if you have columns which are named created_at/created_on or updated_at/updated_on, rails will automatically update the timestamps for you when records are inserted / updated. Also things like columns named 'id' are understood by ActiveRecord as primary keys and columns named tablename_id are understood as foreign key references.

For working with existing tables, you can override these conventions, telling active record to use alternate columns for these actions.

The procedure is very simple.
1. Create a new rails project.
2. Edit your_project_name/config/database.yml and set up the connection parameters so that it connects to your existing database.
3. Type the following for each table in your existing database:


script/generate model tablename

and substitute tablename for each table in your database. This will create ruby objects to access the existing tables in your database.

As you can see, there's nothing complicated about the procedure.

Hi, i was having this same problem and ran in to this post, problem i'm having now is, where do i type the script/generate.....
i tried it on the same database.yml and it gives a syntax error and i tried it on the Ruby Shell and it gives me another type of error
>> script/generate model metodos
script/generate model metodos
SyntaxError: (irb):15: , unexpected tIDENTIFIER

i'm using the netbeans IDE if that is of any help.

Hi, i was having this same problem and ran in to this post, problem i'm having now is, where do i type the script/generate.....
i tried it on the same database.yml and it gives a syntax error and i tried it on the Ruby Shell and it gives me another type of error
>> script/generate model metodos
script/generate model metodos
SyntaxError: (irb):15: , unexpected tIDENTIFIER

i'm using the netbeans IDE if that is of any help.

type it in your OS console (while in your script's root dir) not in the Ruby console.

type it in your OS console (while in your script's root dir) not in the Ruby console.

cool thanks :)










privacy (GDPR)