What Exactly Does the Serializer Do?

What Exactly Does the Serializer Do?

Ruby on Rails as an API Backend with JavaScript Frontend. Need to use a serializer.

After dabbling in Ruby on Rails and venturing into JavaScript, I found it very useful to have Rails as the backend API. It is setup in such a way where it can easily render JSON data to be used on the JavaScript side and therefore the browser.

Building my app was going well until it came time to render attributes of a model in relation to another model.

In Ruby, the relationships are set up like so and through naming conventions it knows where to look for whatever it’s looking for. Here, I have 3 models in a many-to-many relationship.

class Review < ApplicationRecord

  belongs_to :user
  belongs_to :wine
end
class User < ApplicationRecord
  has_many :reviews
  has_many :wines, through: :reviews
end
class Wine < ApplicationRecord
  has_many :reviews, :dependent => :destroy
  has_many :users, through: :reviews
end

In a Rails app, to create the frontend I would just need to create html.erb files within a folder (within app/views folder) that correlates with the above models by sharing the same name (naming convention). Then, in the controller actions I would just need to specify what to render. Through naming convention of the files, Rails automatically (almost magically) knows what is linked to what and therefore what to render.

But, I’m venturing into using JavaScript/HTML as my frontend, remember?

So, in the controller actions I need to tell the app what to render (via ‘render json:’ then whatever specified to render).

javascript javascript-tips active-model-serializer rails serializers

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

7 Simple JavaScript Tips for Optimizing Your Code

You may not know about these tips 7 Simple JavaScript Tips for Optimizing Your Code

Javascript Pro Tips

In this article, I am going to talk about Javascript Pro Tips. Please learn how to write solid modern Javascript and avoid bad code from the olden days.

JavaScript Shopping Cart - Javascript Project for Beginners

JavaScript Shopping Cart - javascript shopping cart tutorial for beginnersBuy me a coffee 🍺 https://www.paypal.com/paypalme/ziddahSource Code: https://bit....

JavaScript Memory Management System

The main goal of this article is help to readers to understand that how memory management system performs in JavaScript. I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.