What is a Serializer?
A Serializer is an ActiveModel class which defines how the data of its corresponding model is distributed. Basically, when any part of your Rails app wants to render something, it looks to the serializer.
Let’s say we are building a forum app where
Users can make
Posts. In ActiveRecord terms: a user
has_many :posts while a post
belongs_to :user. Here is a basic example of how the serializers would look:
class PostSerializer < ActiveModel::Serializer
attributes :id, :message
endclass UserSerializer < ActiveModel::Serializer
attributes :id, :display_name, :username
With this information, we know that when a
Whether you’ve been programming for 5 years or 5 months, there is no way you haven’t copied a line of code or two… or two hundred. However, those brand new to programming might adopt the misconception that using someone else’s code is categorically bad. Let me get this out of the way first: there is nothing wrong with using code found online…if you do it right. Well I shouldn’t say right, there are good practices to copying code that yield much better results than others. I’m here to help shed some light on some good practices that I’ve learned.
Begin with the end in mind
Have you ever had a big project or assignment where the first thing you did was open a blank canvas and stare at the cursor? In fact, that’s exactly what I did when preparing to write this blog. It feels like you know where you want to end up but there are a thousand paths you could take. Luckily, I’ve learned a few ways to combat that paralyzing feeling and get the ball rolling as painlessly as possible.
Like I said before, often times, we know where we want to end up but not…
Variables are sort of like boxes we can store data in. Once they are in this “box” it has a name you can reference at any time. For instance, you could store a kitchen supply, fruit, vegetables, or a ball — anything.
let kitchenSupply = "knife";
let fruit = "banana";
let vegetable = "broccoli";
let ball = "soccer";
What you store in the box doesn’t even need to be related to its name. We could even use the same names and put random data in the variables:
let kitchenSupply = true; //Boolean
let fruit = 10…