<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jennifer Lao on Medium]]></title>
        <description><![CDATA[Stories by Jennifer Lao on Medium]]></description>
        <link>https://medium.com/@jenn.p.lao?source=rss-75331a9983f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*DsopXgrkMwXtIDRS</url>
            <title>Stories by Jennifer Lao on Medium</title>
            <link>https://medium.com/@jenn.p.lao?source=rss-75331a9983f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 14:02:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jenn.p.lao/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Rales Engine Reflection]]></title>
            <link>https://medium.com/@jenn.p.lao/rales-engine-reflection-f256a52d8f3?source=rss-75331a9983f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f256a52d8f3</guid>
            <category><![CDATA[activerecord]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jennifer Lao]]></dc:creator>
            <pubDate>Mon, 03 Dec 2018 00:34:39 GMT</pubDate>
            <atom:updated>2018-12-03T03:43:30.027Z</atom:updated>
            <content:encoded><![CDATA[<p>Rales Engine [sic] was my first real project converting imported data into JSON. We had to create record endpoints, relationship endpoints, and business intelligence endpoints for the given data in just one week. There were 6 total resources: merchants, customers, invoices, items, invoice items, and transactions that were all connected to each other through various relationships.</p><p>At first, it seemed like we were being asked to climb a 14er when our daily exercise routine consisted of climbing a flight of stairs. But, by taking a little bit of time to ‘train’, the climb was not as daunting as it first seemed.</p><p>The first step of ‘training’ was to break the project down into smaller pieces and set goals. I broke business intelligence endpoints into smaller portions of: <em>all merchant</em> business intelligence, <em>individual</em> <em>merchant</em> business intelligence, <em>item</em> business intelligence, and <em>customer</em> business intelligence. On the first day, I created the record endpoints and set up the relationships on the model level. Then, I shot for finishing one portion of business intelligence and one resource’s relationship endpoints each day. As I made progress with the business intelligence endpoints, I found that I was getting faster at recognizing which ActiveRecord Methods should be called.</p><p>Using ActiveRecord queries, we had to create endpoints that could return things such as:</p><ul><li>Merchants ranked by total revenue</li><li>Items ranked by total number sold</li><li>Merchant who a specific customer ordered from the most</li></ul><p>To break the business intelligence piece down, diagramed each problem:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ObNn-sDE9zjkAlpmb1Hm7A.jpeg" /></figure><p>First, (in black) I wrote out all the names of the resource tables and the columns I would need from each table. Then I looked at how I would use each of those resources. Finally, I wrote out the ActiveRecord methods (in red) that I would be using on them. This process helped me write methods using ActiveRecord that looked like this:</p><pre>def self.total_revenue(quantity)<br> select(‘merchants.*, sum(invoice_items.quantity * invoice_items.unit_price) as revenue’)<br> .joins(:invoices, invoices: [:invoice_items, :transactions])<br> .where(transactions: {result: ‘success’})<br> .group(:id)<br> .order(‘revenue desc’)<br> .limit(quantity)<br> end</pre><p>The best part of this project was the repetition for all the relationship and record endpoints. With 6 different resources I gained a lot of muscle memory during my training of how serializers work and how to get them to display the information you want. For example, Merchants don’t have an attribute for their revenue, but it can be calculated. Below is an example of how you can get a serializer to display the revenue for a specific merchant.</p><pre>class Api::V1::Merchants::RevenueController &lt; ApplicationController<br>  def show<br>    render json:    <br>       MerchantRevenueSerializer.new(Merchant.revenue(params[:id]))<br>  end<br>end</pre><pre>class Merchant &lt; ApplicationRecord<br>  def self.revenue(id)<br>    select(&#39;merchants.*, sum(invoice_items.quantity * invoice_items.unit_price) as revenue&#39;)<br>    .joins(:invoices, invoices: [:invoice_items, :transactions])<br>    .where(transactions: {result: &#39;success&#39;})<br>    .group(:id)<br>    .where(id: id)<br>    .first<br>  end<br>end</pre><pre>class MerchantRevenueSerializer<br>  include FastJsonapi::ObjectSerializer<br>  attribute :revenue do |object|<br>    (object.revenue / 100.00).to_s<br>  end<br>end</pre><p>The show method in the RevenueController calls the method revenue on the Merchant class and passes in an argument of a merchant’s id. In the Merchant class, the revenue method aliases the sum(invoice_items.quantity * invoice_items.price) as revenue. This in essence creates a column called revenue in the Merchant’s table that holds this calculated number that is now accessible. Lastly, you create an attribute with the name revenue and with each object (in this case a Merchant) use the serializer to make it appear in the attributes. Your output would look like this:</p><pre>{<br>    &quot;data&quot;: {<br>        &quot;id&quot;: &quot;27&quot;,<br>        &quot;type&quot;: &quot;merchant_revenue&quot;,<br>        &quot;attributes&quot;: {<br>            &quot;revenue&quot;: &quot;483105.56&quot;<br>        }<br>    }<br>}</pre><p>Looking back at the project, I believe that using more TDD, especially while working on the business intelligence piece, and viewing my JSON output using the Postman app would have reduced some of my headaches. Often, I would get the right output, but inside an array and got an error of ‘No implicit conversion of string into integer’. Had I viewed my output using Postman, I would have sooner seen what my problem was. In future projects, I will be sure to ‘Always, always, always, write tests first’.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f256a52d8f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Differences Between Joins]]></title>
            <link>https://medium.com/@jenn.p.lao/the-differences-between-joins-81a25c9e9260?source=rss-75331a9983f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/81a25c9e9260</guid>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Jennifer Lao]]></dc:creator>
            <pubDate>Wed, 07 Nov 2018 14:39:45 GMT</pubDate>
            <atom:updated>2018-11-07T16:18:26.728Z</atom:updated>
            <content:encoded><![CDATA[<p>Joins in SQL can be hard to understand, at first, if you have little-to-no programming or data analysis experience. As a current student in a 7 month Backend Engineering program with no previous programming experience, I found them to be a bit challenging to conceptualize, until I looked at this picture:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*VjHyEhhdInSRvDww.png" /></figure><p>I know it still looks a little confusing but let’s break it down a little and see if we can make joins a little easier to understand.</p><p>I’ll tap into my passion for comics to illustrate what joins can be used for. Let’s say we have two tables, one with Heroes and one with Superhero Teams. A Hero can belong to a team so the Heroes table will consist of 3 columns: 1) Their alias (e.g. “Wolverine”), 2) Their real name (e.g. “Logan”), and 3) the superhero team they’re most associated with (in Wolverine’s case, the “X-Men”). For good measure, we’ll throw in some heroes that usually roll solo and aren’t really associated with a particular team, like Daredevil. We will also have a team that does not have any heroes it (maybe all of them quit?). The Team table will have 3 columns as well: 1) The team name (e.g. “Avengers”), 2) their headquarters (e.g. “Avengers Mansion”), and 3) the location of said headquarters (e.g. “New York City). So here are our tables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mZ4i9dzg2LuTkdVynKWIEQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qp0pkqgvtXiazWjyVU2v9g.png" /></figure><h3>Inner Join also called Join:</h3><p>An inner join looks at two tables and returns the data where there is a match in both tables. If you created an inner join between our two tables, you would get a table that looks something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*plawxmwAqBoQ5r7Pr7w7CQ.png" /></figure><p>The SQL for this table would be:</p><p>SELECT hero.*, team.* FROM hero JOIN team ON hero.team_name = team.name;</p><p>The most important takeaway is that you would return a table with only heroes that have teams and only teams that have heroes. Sorry, Daredevil :(</p><h3>Left Outer Join also called Left Join:</h3><p>A left outer join preserves all of the left table (the Heroes) and records that have no relation in the right table (the Teams) will contain the value null. So using SQL that looks like this:</p><p>SELECT hero.*, team.* FROM hero LEFT OUTER JOIN team ON hero.team_name = team.name;</p><p>You’d get a table that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6PuVKIheoPDodUIoxlxDoQ.png" /></figure><p>As you can see we still have all 12 heroes , but the heroes that have no team (Daredevil, Elektra, and Ghost Rider) now have null values in the columns that correspond to the Team table.</p><p>Now say you only want to see the Heroes that run solo. All you have to do as add in a where clause.</p><h3>Left Outer Join where B.key is Null</h3><p>The SQL for joining the hero table with the team table but only getting back the heroes without a team this would look like this:</p><p>SELECT hero.*, team.* FROM hero LEFT OUTER JOIN team ON hero.team_name = team.name WHERE team.name = null;</p><p>which would create a table that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BucnHAJy8ro8_aO0IP1wWw.png" /></figure><p>As you can see, our data got a lot smaller and we only see the Heroes that are not on a team.</p><h3>Right Outer Join also called Right Join:</h3><p>This works the same way as Left Outer Join but preserves the right table (in our case, the Team table) and has null values in the left table (the Hero table) where no match exists.</p><p>In SQL:</p><p>SELECT hero.*, team.* FROM hero RIGHT OUTER JOIN team ON hero.team_name = team.name;</p><p>gives you a table that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8GKqHX7ZQ8acUdrDXzDfOQ.png" /></figure><p>As you can see, all the information from all the teams is in tact, but the heroes that run solo are gone from our table and there are null values on the Hero side that correspond to the team Guardians of the Galaxy.</p><p>And, similar to left outer join, if you only wanted to see the the teams with no corresponding heroes, all you’d have to do is add in a where clause.</p><h3>Right Outer Join where A.key is Null</h3><p>The SQL for this would look like this:</p><p>SELECT hero.*, team.* FROM hero RIGHT OUTER JOIN team ON hero.team_name = team.name WHERE hero.team_name = null;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IaMjcqzXB7i1TfdJc1iuHQ.png" /></figure><p>Now we only see teams with no heroes.</p><p>Now you’re probably thinking, what if I want to see all the heroes and all the teams? Well, that’s where Full Outer Join comes in.</p><h3>Full Outer Join:</h3><p>To return all the data from both the hero and the team tables you’d use a SQL command like this:</p><p>SELECT hero.*, team.* FROM hero FULL OUTER JOIN team ON hero.team_name = team.name;</p><p>and you’d get a table that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XiTThFQ-6ZyQXkoAmve9VQ.png" /></figure><p>Yay! We have all the heroes and all the teams!</p><p>But what about if I only wanted to see the heroes without teams and the teams without heroes? All you have to do is add in a where clause!</p><h3>Full Outer Join where A.key is Null or B.key is Null</h3><p>So to find all the heroes who do not have a teams and all the teams that don’t have hero you’d use this SQL:</p><p>SELECT hero.*, team.* FROM hero FULL OUTER JOIN team ON hero.team_name = team.name WHERE hero.team_name = null OR team.name = null;</p><p>and we’d get a table that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EhFisNCP_T3ZgcP4kY1yNw.png" /></figure><p>Now we only see Heroes without a team and teams without a hero.</p><p>That’s all the options for joining tables. Hope this helps you understand the differences between all the different joins. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81a25c9e9260" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>