Serializers with Custom Methods — Using the Back End to control the flow of information

Jay Lee
4 min readMar 11, 2022

Phase 4 of Flatiron School’s Software Engineering program expanded on Phase 3, where we learned about Ruby, Active Record, and Sinatra to build backend APIs. In Phase 4, our ability to build out the backend was expanded by learning about Ruby on Rails, which is a Ruby framework that makes building backends easier and more robust, which pairs well with React to build the frontend.

While React gives the ability to alter how the data from the backend is presented, Ruby on Rails allows you to control how that data is even sent to the Frontend. In my boardgame library app, Meeples for Peeples, I utilize this to create a better user experience.

I utilized Material UI to use Card components to present information about each board game, but in the Card, I chose to keep the description shorter to not overwhelm the user when other cards are already rendered, but the information is still important to present. This was all presented through the Index route which is reserved to show the information of all items (in this case for the BoardGame class).

I coded in a Modal window tied to a Show route (which is reserved to show the information from one item in the BoardGame class). Using a custom serializer I was able to control using a custom method to present the whole description in the Modal window.

This all starts with the BoardGameController which controls what each route does. How the information is presented is from the serializer, which by default is called BoardGameSerializer (thank you Rails for improving the coder experience by making this the default serializer that is also created by the Resource generator). The serializer controls what columns and how they are presented from the BoardGame database.

This is all the data that is held in the database, and although all of it is valuable, not all of it is necessary at all times. That BoardGameSerializer allows the user not to be overwhelmed by unnecessary information.

In the BoardGameSerializer, I coded a custom method Summary, that presents the first 250 characters of the description of each BoardGame. By also adding Summary as an attribute at the top of the code, the serializer knows to present Summary rather than Description.

The Show route has a custom serializer that is tied to it to change the information that is provided.

The BoardGameSerializer used the custom method Summary to restrict how much of the description was sent to the Frontend. For the Show route though, the user now can see the full description rather than the 250 characters that the Summary method called for. Additionally, as you can see in the three above screenshots, there is also a custom serializer for the User class, that controls how the User information that the BoardGame class can call on through association.

The information in a database although important to the user is often less important than how it is presented to the user. Thanks to Controllers, Serializers, and custom methods, the developer can control the narrative.

This ability in Ruby on Rails reminds me of one of my favorite quotes from Maya Angelou:

“I’ve learned that people will forget what you said, people will forget what you did, but people will never forget how you made them feel.”

Thank you Rails developers for helping control the flow of information to make the user feel a specific way.

--

--

Jay Lee

software/support engineer with a health/wellness coaching addiction and an awesome dog based in Brooklyn