DEV Community

M-Shkreli21
M-Shkreli21

Posted on

Serializers with Ruby on Rails

With our final learning phase at the Flatiron School we began by learning about Rails, which is a library used for the Ruby language. Rails has done an amazing job of simplifying and automating a lot from plain old ruby. One of my favorite things about rails is the inclusion of serializers for the data that exists in your backend. Serializers are files within your app that allow us as developers to customize how users will see the data that exists. When creating our tables in rails we can enter 'Rails g resource [tablename]' and the serializer file will automatically be created.

After the file has been created it will default to show every column in your table (except the timestamps), from here we can edit which attributes we want to show when viewing data in our app. One thing that I have noticed most people take off is showing the id column from each table, since users themselves don't typically need to see the id for each data point brought in.

Serializers can do a lot more than just edit one tables data view. If associations exist between tables whether they are many-to-many, one-to-many, etc. we are able to show that relationship through serializers.

Think of a blogging application, a user can have multiple blog posts but each blog post belongs to one user. In our serializers we can establish the relationship like normal, users: has_many :posts & posts: belongs_to :user. From here we can see that when we use a tool like postman to view users we can see the associated posts that exist for each respective user. Even with associations like this, we can still filter through which attributes we want out user to see.

Rails has been an amazing resource for me when trying to create full stack applications. The initial setup and modifications make it so simple and helps ruby developers speed up the process in which they design and apply the backend files they need for each project.

Top comments (0)