My personal journey with custom serializers has been an interesting one. In my 15 week coding bootcamp, in the 4th phase, we had a code challenge where we are tested on the material that we had been taught the prior week and a half. In my code challenge there was one thing that I forgot and ultimately held me back from passing on my first of two attempts; Custom Serializers.
A custom serializer, is a version of a traditional serializer that you can just create for certain scenarios or display purposes that you may have. Creating a custom serializer with Ruby on Rails is as simple as the command
rails g serializer /YourSerializerName/
A custom serializer works in the exact same way that any other serializer might, just with the caveat that you specifically call the serializer on a method, instead of importing the serializer and using it as a blanket on all the methods in a controller. Such as…
def index bears = Bear.all render json: bears, serializer: CustomBearSerializer, status: :ok end
This is actually quite simple to add into your methods, but there is a lot of power that you can give your custom serializer. You could use it to display associated data with each hash that you render from your ruby application, you can change the format that it displays in, or even change the data output itself, either adding more data to a specific output or displaying less data for another. Like this!
BearSerializer attributes: :name, :weight, :packname
Let’s say this is the basic serializer you are using to display your data in your output or input. No matter which method you decide to create, it will output your data with this information. But what if when you ask for a specific bear by id, you just wanted to add a list of that bears cubs? Well, then you would need a custom serializer to invoke on that show method! Which would look a little something like this.
CustomBearSerializer attributes: :name, :weight, :packname has_many :cubs
Assuming you have a Bear and Cub relationship and models established, This custom serializer above is all you need to add the list of cubs to your output, now let’s go add that serializer to our show method like we did in the index example before!
def show bear = Bear.find(params[:id]) render json: bear, serializer: CustomBearSerializer, status: 200 end
There we go! Now when we call the show method by calling for a specific bear by id number, it will also render the list of cubs that belong to that bear. Since this custom serializer was only called on that one show method, that will be the only time that the data includes the list of cubs!
This was a very basic version of custom serializers, but they will be very useful to you when coding with rails to get your data to render the exact way that you want it to for each situation. After my strange hiccup with this rails topic, I hope this helps you to have a better understanding of the concept, whoever may be reading this. Practice and toy around with it and see what you can get to display in your own code! Good luck and, as always, happy coding!!