How do we add the Active Model Serializer to Ruby on Rails?
Create a new Rails API application named “simple-rest-api” using the rails new
command.
rails new simple-rest-api --api
cd simple-rest-api
Generate the User, Book, and Subject models using the rails generate model
command.
rails generate model User username:string password:string
rails generate model Book user:references book:string likes_books:integer
rails generate model Subject book:references subject:string likes_subjects:integer
Set up associations between the models by defining the relationships in the corresponding model files (app/models/user.rb
, app/models/book.rb
, and app/models/subject.rb
).
Run the database migrations to create the necessary tables.
rails db:migrate
Generate controllers for the API endpoints using the rails g controller
command.
rails g controller api/v1/Users
rails g controller api/v1/Books
rails g controller api/v1/Subjects
Define the routes for the API endpoints in the routes.rb
file, specifying the nested resource structure.
namespace :api do
namespace :v1 do
resources :users do
resources :books
resources :subjects
end
end
end
Add the active_model_serializers
gem to the Gemfile and run bundle install
.
gem 'active_model_serializers'
bundle install
Generate a serializer for the User model using the rails g serializer
command.
rails g serializer user
Add the seed data to populate the database with dummy user records.
user = (1..50).map do
User.create!(
username: "user",
password: "123456"
)
end
Run the seed task to insert the data into the database.
rails db:seed
Implement the index action in the UsersController
to fetch all users and render the response in JSON format using the Active Model Serializers gem.
def index
@users = User.all
render json: {
data: ActiveModelSerializers::SerializableResource.new(@users, each_serializer: UserSerializer),
message: ['Employee list fetched successfully'],
status: 200,
type: 'Success'
}
end
This setup allows you to build a simple REST API for managing users, books, and subjects with their respective associations. The code provided demonstrates the basic structure and functionality, but you can expand on it to add more actions, validations, and error handling as per your requirements.
Run rails s and browser hit this URL http://localhost:3000/api/v1/users
We are getting below output
{"data":[{"id":1,"username":"bhartee","password":"123456"},{"id":2,"username":"user","password":"123456"},{"id":3,"username":"user","password":"123456"},{"id":4,"username":"user","password":"123456"},{"id":5,"username":"user","password":"123456"},{"id":6,"username":"user","password":"123456"},{"id":7,"username":"user","password":"123456"},{"id":8,"username":"user","password":"123456"},{"id":9,"username":"user","password":"123456"},{"id":10,"username":"user","password":"123456"},{"id":11,"username":"user","password":"123456"},{"id":12,"username":"user","password":"123456"},{"id":13,"username":"user","password":"123456"},{"id":14,"username":"user","password":"123456"},{"id":15,"username":"user","password":"123456"},{"id":16,"username":"user","password":"123456"},{"id":17,"username":"user","password":"123456"},{"id":18,"username":"user","password":"123456"},{"id":19,"username":"user","password":"123456"},{"id":20,"username":"user","password":"123456"},{"id":21,"username":"user","password":"123456"},{"id":22,"username":"user","password":"123456"},{"id":23,"username":"user","password":"123456"},{"id":24,"username":"user","password":"123456"},{"id":25,"username":"user","password":"123456"},{"id":26,"username":"user","password":"123456"},{"id":27,"username":"user","password":"123456"},{"id":28,"username":"user","password":"123456"},{"id":29,"username":"user","password":"123456"},{"id":30,"username":"user","password":"123456"},{"id":31,"username":"user","password":"123456"},{"id":32,"username":"user","password":"123456"},{"id":33,"username":"user","password":"123456"},{"id":34,"username":"user","password":"123456"},{"id":35,"username":"user","password":"123456"},{"id":36,"username":"user","password":"123456"},{"id":37,"username":"user","password":"123456"},{"id":38,"username":"user","password":"123456"},{"id":39,"username":"user","password":"123456"},{"id":40,"username":"user","password":"123456"},{"id":41,"username":"user","password":"123456"},{"id":42,"username":"user","password":"123456"},{"id":43,"username":"user","password":"123456"},{"id":44,"username":"user","password":"123456"},{"id":45,"username":"user","password":"123456"},{"id":46,"username":"user","password":"123456"},{"id":47,"username":"user","password":"123456"},{"id":48,"username":"user","password":"123456"},{"id":49,"username":"user","password":"123456"},{"id":50,"username":"user","password":"123456"},{"id":51,"username":"user","password":"123456"}],"message":["Employee list fetched successfully"],"status":200,"type":"Success"}