Restful APIs and Model creation in Ruby on Rails


Active Record plays the most important role in ROR. Active Record is the ORM (Object-relational mapping) of rails. Using the Active Record we can perform CRUD operation in ROR and manages all database related operations. It is a model aspect of rails and it is representative of data. Is allows you to present data from databases rows as objects and used in business logic
1. Create models:
We can create models in ROR using rails like π
rails generate model model_name field_name:type, field_name:type...
We will create some sample models for your understanding
Company model:
1. rails generate model Company name:string email:string address:string
Adding foreign key in the Employee model:
rails generate model Employee name:string emp_id:string company:references
here I have added the company primary key as an FK.
Here π is a sample demo:
It creates a migration file in the db/migrate folder. After that, we can migrate all created model into our selected database(MySQL, Postgres, etc)
Using command π
rake db:migrate
It migrates all the models into our database. Yeah! here we have created models. Now we will perform CRUD operations.
2. CRUD
Here we will create api/v1 folder under the app/controller folder and will start writing our APIs finally π₯
In the app/controller/api/v1 folder, I have created a file named company_controller.rb π
Also In the same folder, I have created a file named employee_controller.rb π
Here we have created all CRUD operations related to the company model and the employee model. also did pagination in the company index function with proper error handling and logging.
A. In ROR we perform error handling using π
begin
//some code
rescue => e
end
B. Rails provide logging functionality with its own, you just use
Rails.logger.{type} type:(info, warn, debug etc,)
C. You can also create fake data. For this purpose, you add the gem βfakerβ in Gemfile then run bundle install.
For creating fake data you can write code in db/seeds.rb file like π
5.times doCompany.create({name: Faker::Name.name,email: Faker::Internet.email,address: Faker::Book.title})end5.times doEmployee.create({name: Faker::Name.name,emp_id: Faker::Number.number(digits: 10),company: 1})end
migrate fake data into the database
rails db:seed => used for migrate fake data
Creates Routes:
You can create routes in config/routes.rb file
Rails.application.routes.draw domount Rswag::Ui::Engine => '/api-docs'mount Rswag::Api::Engine => '/api-docs'# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.htmlnamespace 'api' do // folder name that we have created in controller folder for CRUDnamespace 'v1' doresources :company post 'company/search' // custome routepost 'company/search_query'resources :employeeendendend
Testing:
ROR provides routes. We can check all routes of ROR using:
rails routes => show all routes
For testing, you can refer to my previous blog.
Conclusion:
Yeah! π we finally have done all CRUDs and Models π₯
Subscribe to my newsletter
Read articles from NonStop io Technologies directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

NonStop io Technologies
NonStop io Technologies
Product Development as an Expertise Since 2015 Founded in August 2015, we are a USA-based Bespoke Engineering Studio providing Product Development as an Expertise. With 80+ satisfied clients worldwide, we serve startups and enterprises across San Francisco, Seattle, New York, London, Pune, Bangalore, Tokyo and other prominent technology hubs.