<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: LanceHebert</title>
    <description>The latest articles on DEV Community by LanceHebert (@lancehebert).</description>
    <link>https://dev.to/lancehebert</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F811104%2F4236e034-917d-40da-8a1d-bf02765521e7.jpeg</url>
      <title>DEV Community: LanceHebert</title>
      <link>https://dev.to/lancehebert</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lancehebert"/>
    <language>en</language>
    <item>
      <title>The Basics of Serializer: A Rails guide</title>
      <dc:creator>LanceHebert</dc:creator>
      <pubDate>Tue, 08 Feb 2022 02:22:10 +0000</pubDate>
      <link>https://dev.to/lancehebert/the-basics-of-serializer-a-rails-guide-200p</link>
      <guid>https://dev.to/lancehebert/the-basics-of-serializer-a-rails-guide-200p</guid>
      <description>&lt;p&gt;Active Model Serializer is a gem that we can include with our rails app to improve control of data structure. With API calls, it is important to control what information is seen by the user and what info is extraneous and doesn't need to be passed along. &lt;/p&gt;

&lt;p&gt;Let's run through an example for better understanding.&lt;/p&gt;

&lt;p&gt;First,  make sure to install the gem in your Gemfile, and then run &lt;code&gt;bundle install&lt;/code&gt; to make sure the gem is active.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem "active_model_serializers", "~&amp;gt; 0.10.12"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, set up a table with our future information. In this example we have a many-to-many relationship utilizing a join table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sMmo_Uzf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1na71e7nqj1dsb5gcvnw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sMmo_Uzf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1na71e7nqj1dsb5gcvnw.png" alt="Many to Many table" width="880" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After generating each model, controller, and modifying routes as needed, go ahead and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate serializer &amp;lt;Name of table&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--maJvPO8j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w47dkocr5afu62pyej4u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--maJvPO8j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w47dkocr5afu62pyej4u.png" alt="initial Camper Serializer" width="880" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s5-qoyEF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pftswuiltf362uzcwew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s5-qoyEF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pftswuiltf362uzcwew.png" alt="Initial Get Request with only :id" width="880" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Be aware that serializer uses convention over configuration and also CamelCases its class name, even if it is made with snake casing. &lt;/p&gt;

&lt;p&gt;At this point what the above image is saying is that whenever the camper controller is responding to a route endpoint it will use the format of the serializer. Since only the &lt;code&gt;:id&lt;/code&gt; is listed any camper endpoint used will return just the &lt;code&gt;:id&lt;/code&gt; associated with it. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DCMT4zNL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifguv6stzb69ps4ytgru.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DCMT4zNL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifguv6stzb69ps4ytgru.png" alt="Serializer with attributes added" width="880" height="215"&gt;&lt;/a&gt;&lt;br&gt;
Here we have increased the data returned by including more info from our table.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--20613iOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0w46llrsd6xuv1kga32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--20613iOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0w46llrsd6xuv1kga32.png" alt="Attributes returned from get request" width="880" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also tap into the relationships we have set between the tables. &lt;/p&gt;
&lt;h2&gt;
  
  
  Nesting Data with Relationships
&lt;/h2&gt;

&lt;p&gt;A quick way to incorporate the relationships into the data you get back is to use the built in &lt;em&gt;convention over configuration&lt;/em&gt; of Serializer. To do this, merely use the relationship to the other table in the serializer like this &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1w6MANlV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq4mct2fjnxkwivbeiqs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1w6MANlV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq4mct2fjnxkwivbeiqs.png" alt="Serializer with added relationship" width="880" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---VrhCe0_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dei2j3s1rew3ts93jdds.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---VrhCe0_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dei2j3s1rew3ts93jdds.png" alt="Serializer with added relationship" width="880" height="502"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This allows us to use the relationships established in our tables to return back the activity data of the selected Camper.&lt;/p&gt;

&lt;p&gt;What if we want to include different data for each Camper route(show, index ..etc) ? &lt;/p&gt;
&lt;h2&gt;
  
  
  Explicit Specifying of Serializer
&lt;/h2&gt;

&lt;p&gt;Let us say we want our index route to just show our Campers with no other associated data but we would like our Show route to include activities that a specific camper enjoys. We can enable that with another custom serializer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate serializer Camper_Activity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the needed attributes, and then head to the camper controller.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BUi5Wrqn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugyq3so9zv91t0fqdk8s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BUi5Wrqn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugyq3so9zv91t0fqdk8s.png" alt="Controller with explicit serializer added" width="880" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This tells our camper controller to override its normal use of &lt;strong&gt;CamperSerializer&lt;/strong&gt; and use &lt;strong&gt;CamperActivitySerializer&lt;/strong&gt; instead. This means that when the &lt;strong&gt;Camper#show&lt;/strong&gt; route is called on it will display the data how we established in &lt;strong&gt;CamperActivitySerializer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, one last useful element we can use to really control  data format in &lt;em&gt;Active Model Serializer&lt;/em&gt; is custom methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Methods
&lt;/h2&gt;

&lt;p&gt;Custom methods allow you to add methods in to modify existing data to produce unique outputs to include back in the response. For example, let's say we wanted to include average difficulty of activities for a camper. &lt;/p&gt;

&lt;p&gt;First, add the attribute which will be the name of the custom method as a symbol.&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GLKzSAcQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aclek8vn7ublpb436wo7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GLKzSAcQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aclek8vn7ublpb436wo7.png" alt="Added attribute to serializer" width="880" height="184"&gt;&lt;/a&gt;&lt;br&gt;
Then add your custom method to the model.&lt;br&gt;
 &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2EOjcZVm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkuhkz6kqemwls0awzhn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2EOjcZVm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkuhkz6kqemwls0awzhn.png" alt="Custom method in Camper Model" width="880" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This enables you to now include in the response the difficulty average of the Camper's activities by utilizing the power of AMS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ekJamPCm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhyxnbc5l3prgtdqsgog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ekJamPCm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhyxnbc5l3prgtdqsgog.png" alt="Final get request with Difficulty Average included" width="880" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hopefully this blog has given you a better understanding of some of the uses of Active Model Serializer that you can use in your future projects!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
