Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to build rails api with postgresql

I know nothing about PostgreSQL and a little about Ruby on Rails. I know Ruby. I have experience with the command line.

I'm interning and I was told to build a Rails API that loads data into a two-dimensional array from PostgreSQL database. The API is meant to have methods to get certain pieces of the data given a certain index.

I don't know how/where to begin despite how simple it should be. I have no experience with databases. Please point me in the right direction to get this set up and started.

like image 525
user3342460 Avatar asked Aug 30 '25 18:08

user3342460


1 Answers

Starting with Rails 5, you can generate a Rails API only application. Just use the following command:

rails new project-name-here --api --database=postgresql

I assume that you have ruby, rails and postgresql installed.

rails db:create # creating DB

Running rails generate scaffold User first_name:string last_name:string, it will generate all files needed for User model.

A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.

rails db:migrate # migrating the DB
rails s # starting the server

Now, you can access the /users endpoint. Do not forget to update controllers actions with relevant data.

A detailed example

like image 88
mmsilviu Avatar answered Sep 02 '25 06:09

mmsilviu