<?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>Forem: LaurenTyson🕶</title>
    <description>The latest articles on Forem by LaurenTyson🕶 (@laurentyson85).</description>
    <link>https://forem.com/laurentyson85</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%2F863516%2Fec6c2e3e-5d13-47f3-a8e8-72e00a24b30c.jpeg</url>
      <title>Forem: LaurenTyson🕶</title>
      <link>https://forem.com/laurentyson85</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/laurentyson85"/>
    <language>en</language>
    <item>
      <title>Authenticating Users in Rails</title>
      <dc:creator>LaurenTyson🕶</dc:creator>
      <pubDate>Sat, 14 Jan 2023 03:27:36 +0000</pubDate>
      <link>https://forem.com/laurentyson85/authenticating-users-in-rails-hm1</link>
      <guid>https://forem.com/laurentyson85/authenticating-users-in-rails-hm1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bPmZPrBV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wpualqosrnsm9iirnjx4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bPmZPrBV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wpualqosrnsm9iirnjx4.png" alt="unauthorized" width="880" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Logging in.&lt;/p&gt;

&lt;p&gt;All websites do it, and as internet users, we don't think about logging in or logging out or weird things like creating a "user session." 😵‍💫&lt;/p&gt;

&lt;p&gt;But all of that goes on in the backend in Rails. &lt;/p&gt;

&lt;p&gt;Think of websites like the club; they use a "stamp." You arrive, and the bouncer checks your id and gives you a stamp. If you leave and come back later (...maybe after visiting that White Castle), the bouncer doesn't recheck your id; they just check for your stamp. You can get a drink at the bar because your stamp proves you're old enough and have already been checked in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Website example of the stamp pattern ✋🏽:
&lt;/h2&gt;

&lt;p&gt;You log in at etsy.com. Etsy's servers authenticate you.&lt;/p&gt;

&lt;p&gt;If your credentials are correct, Etsy's servers issue a cookie to your browser.&lt;/p&gt;

&lt;p&gt;If you visit another protected page on Etsy, your browser will show the cookie to the server; the server verifies the cookie and lets you load password-protected info and browse in a logged-in state.&lt;/p&gt;

&lt;p&gt;As developers, we describe this process as authentication. When we talk about authentication in our applications, we're describing how an application can confirm that a user is who they say they are. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to authenticate users in Rails:
&lt;/h2&gt;

&lt;p&gt;For a Flatiron boot camp project, I created a rails app called dogGo! which allows you to view and review dog walkers. &lt;/p&gt;

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

&lt;p&gt;At the core of this app is authentication. We don't want someone walking in the front door of this app without signing up and logging in. So this is how I built that functionality:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user navigates to my homepage, which in my case is React.&lt;/li&gt;
&lt;li&gt;The user enters their username and password.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt;ing to /login  or /signup on the Rails backend&lt;/li&gt;
&lt;li&gt;If the user is signing up, then we are going to &lt;strong&gt;CREATE&lt;/strong&gt; a new user with a secure password. If the user is logging in (checking their stamp) then we are going to &lt;strong&gt;CREATE&lt;/strong&gt; a new session with a cookie:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Sign up&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Sg1fnA26--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/splu59g3hu72kzc8j234.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Sg1fnA26--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/splu59g3hu72kzc8j234.png" alt="signup back" width="558" height="48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LGwN7U_f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgntortb9cve5uegxy5e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LGwN7U_f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgntortb9cve5uegxy5e.png" alt="Sign up front" width="702" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Login&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KAaPbMMy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ga11z8ms2cty6hmex5j5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KAaPbMMy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ga11z8ms2cty6hmex5j5.png" alt="Login Back" width="602" height="42"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt;: Typically, your create login method will look up a user in the database, verify their login credentials, and then store the authenticated user's id in the session.&lt;/p&gt;

&lt;p&gt;What the component might look like on the frontend&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--clZy6wcP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ci54hdpya55rp6xs4s60.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--clZy6wcP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ci54hdpya55rp6xs4s60.png" alt="Login on the front" width="880" height="959"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the user submits the form, they'll be logged in! Our callback function would handle saving the logged in user's details in state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Staying Logged In
&lt;/h2&gt;

&lt;p&gt;We've shown our ID at the door (username) and gotten our stamp (session[:user_id]) from the backend. So our backend has a mean of identifying us with each request using the session hash.&lt;/p&gt;

&lt;p&gt;Our frontend knows who we are, because our user data was saved in state after logging in.&lt;/p&gt;

&lt;p&gt;BUT - what happens if we refresh the page in the browser? Well, the frontend doesn't know who we are anymore since we lose frontend state on page refresh, but the backend know who we are - so we need a way of getting the user data from the backend into state when the page firsts loads. &lt;/p&gt;

&lt;p&gt;First, we need a custom route to retrieve the user's data from the database using the session hash:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zbt36SnL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htfqr37jsfd95csovb71.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zbt36SnL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htfqr37jsfd95csovb71.png" alt="Custom route" width="552" height="50"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And a controller action:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wym3K8or--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/061amm6nuhgwlazwsek1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wym3K8or--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/061amm6nuhgwlazwsek1.png" alt="custom controller route" width="656" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we can try to log the user in from the frontend as soon as the application loads, else return a login screen:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Logging Out
&lt;/h2&gt;

&lt;p&gt;It's simple to make a log out flow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add a new custom route for logging out:&lt;/li&gt;
&lt;li&gt;Add a new action in the session controller, which will clear the username out of the session&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8fqatb3C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72ryk9lbg9qxcd6ef8pw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8fqatb3C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72ryk9lbg9qxcd6ef8pw.png" alt="Log out" width="590" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the code on the frontend. A button in the navbar perhaps:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8eItbgMe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0amgdvbv6otj2o652krt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8eItbgMe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0amgdvbv6otj2o652krt.png" alt="Log out again" width="880" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, setUser callback function would handle removing the information about the user from state.&lt;/p&gt;

&lt;p&gt;Ok, lets all go enjoy those White Castle burgers while we wait in line to get back into the club 🍔&lt;/p&gt;

</description>
      <category>rails</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Using the Faker Gem to Populate your Database</title>
      <dc:creator>LaurenTyson🕶</dc:creator>
      <pubDate>Tue, 01 Nov 2022 21:23:02 +0000</pubDate>
      <link>https://forem.com/laurentyson85/using-the-faker-gem-to-populate-your-database-5afe</link>
      <guid>https://forem.com/laurentyson85/using-the-faker-gem-to-populate-your-database-5afe</guid>
      <description>&lt;p&gt;When building an application, using realistic data is a good idea. But where should you get that data, and how should you add it to your database? There are a few different gems that accomplish this, but Faker is the gem I like and used for a recent Ruby project. Let's walk through how to use it.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Faker 🤔
&lt;/h1&gt;

&lt;p&gt;Faker is a Ruby gem that generates fake data that you can use in your database. Per &lt;a href="https://github.com/faker-ruby/faker" rel="noopener noreferrer"&gt;Faker's GitHub page&lt;/a&gt;, the gem is a "library for generating fake data such as names, addresses, and phone numbers." You can even use the gem to randomize boolean values, Lorem text, and dates - it's convenient if you need a quick way to get your database going. &lt;/p&gt;

&lt;p&gt;To use Faker in your app, follow the instructions in the gem's &lt;a href="https://github.com/faker-ruby/faker" rel="noopener noreferrer"&gt;README&lt;/a&gt;, but I found it straightforward. Just install it in your project's folder via your command line like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flss56i097gvui36plalk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flss56i097gvui36plalk.png" alt="gem install"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After installing, you'll see Faker included in your Gemfile, which you can group like I did with the other gems needed to run your app! You're good to go.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvtowlc2sivweasldcnmx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvtowlc2sivweasldcnmx.png" alt="gemfile"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Seeding your database 🌱
&lt;/h1&gt;

&lt;p&gt;Adding data to your database is called seeding. I think of seeding as planting data into my empty tables. Remember 👉 creating and defining your database tables is called migration, but those migrated tables start empty, so seeding is how you add data to the tables. &lt;/p&gt;

&lt;p&gt;The code used to seed is in its own file called &lt;code&gt;seeds.rb&lt;/code&gt;. There is where you write your Ruby methods to plant your Faker data.&lt;/p&gt;

&lt;p&gt;I needed Faker data to create my app, Hipster Hire. A fun "job board"  that lets users post a job and hire a hipster. I had a ton of fun building this, mainly because the fake hipster data I got was hilarious. A bunch of buzzwords used to make a "hipster bio."? LOL, yes, please. &lt;/p&gt;

&lt;p&gt;My app had two Ruby models, a Job model, and a Hipster model - a Job belongs to a Hipster, and a Hipster has many jobs. My database needed fake but realistic job information like title, company name, position, etc., and Hipster information like name and bio. I also wanted to start with some Hipsters already employed and some jobs open and ready to hire a hipster!&lt;/p&gt;

&lt;h1&gt;
  
  
  Planting your seeds 🪴
&lt;/h1&gt;

&lt;p&gt;It was easy to pick out the Faker data I needed; the gem's GitHub page lets you drill down into the different fake generator categories to see the available methods. I picked the information I wanted and added it to my seed file; more on that in a minute. In GitHub, each Faker line is written, for example, as &lt;code&gt;Faker::Hipster.sentences&lt;/code&gt;. This format means that Faker has a static inner class called Hipster that you're extending. And the information after the dot is the specific method called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Faker::Hipster.sentences #=&amp;gt; ["Godard pitchfork vinegar chillwave everyday 90's whatever.", "Pour-over artisan distillery."]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsa6ddesqq9z84v14uho.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsa6ddesqq9z84v14uho.png" alt="github faker"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From there, how you construct your seed file depends on your table structure and your plan for your app. Once you decide, use the &lt;code&gt;seeds.rb&lt;/code&gt; file to add data to the database, all you need to do is write code that uses Active Record methods to create new database records. &lt;/p&gt;

&lt;p&gt;In my app. I needed 25 open jobs, so the &lt;code&gt;hipster_id&lt;/code&gt; foreign key starts as &lt;code&gt;nil&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxl80z6wstjnl6yt3qat.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxl80z6wstjnl6yt3qat.png" alt="Job"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I also needed ten hipsters with anywhere from 1-3 jobs. Fortunately, in Ruby, the &lt;code&gt;rand()&lt;/code&gt; method accomplishes that - it's a class method that generates a random value between the numbers specified. In this nested method, I'm saying, "generate ten hipsters, and for each hipster created, generate for each of them between 1-3 jobs":&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcps69ji5fvj0et0mb1h4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcps69ji5fvj0et0mb1h4.png" alt="Hipster"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty cool!&lt;/p&gt;

&lt;h1&gt;
  
  
  Fake it until you make it 🤭
&lt;/h1&gt;

&lt;p&gt;One tip I learned in my coding bootcamp, Flatiron, is to add...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts "🌱 Seeding..."

[seed code here…]

puts "✅ Done seeding!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...at the top and bottom of my &lt;code&gt;seeds.rb&lt;/code&gt; file because seeding your database doesn't inherently produce anything in your terminal. So to know when the seeding starts and when it's finished, add some simple output like this 👆&lt;/p&gt;

&lt;h1&gt;
  
  
  The final steps ✅
&lt;/h1&gt;

&lt;p&gt;In my app, I'm using Sinatra, a lightweight framework written in Ruby. It's simple but powerful and flexible, so to populate my database, I ran this command after migrating my tables:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bundle exec rake db:seed&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Pro-tip 👉 if I ever need to remove all the data from all my existing tables and rerun the seed command, I have this command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bundle exec rake db:seed:replant&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's it, and my database tables turned out great! Perfectly randomized fake hipster data. Rad 🤘&lt;/p&gt;

&lt;p&gt;PS. this is a snippet of how my Hipster Hire app turned out. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.makeagif.com%2Fmedia%2F11-01-2022%2FLOLAoc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.makeagif.com%2Fmedia%2F11-01-2022%2FLOLAoc.gif" alt="Hipster Hire"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Constructing Search &amp; Filter Functionality in React 🦄</title>
      <dc:creator>LaurenTyson🕶</dc:creator>
      <pubDate>Tue, 30 Aug 2022 18:49:08 +0000</pubDate>
      <link>https://forem.com/laurentyson85/constructing-search-filter-functionality-in-react-dnd</link>
      <guid>https://forem.com/laurentyson85/constructing-search-filter-functionality-in-react-dnd</guid>
      <description>&lt;p&gt;Search and filter functionality is straightforward, yet I often need to refer back to documentation. The biggest thing to remember is the information flow between the components - where does state live, where it's passed, and sending data from a child component to its parent via a callback function. At first, this functionality can feel like a confusing loop, but after you construct components like these a few times, the basic steps get easier to remember.&lt;/p&gt;

&lt;p&gt;Recently I built a React app from scratch as a coding Bootcamp project. The app is called Scientific Foodie, a database that shares scientific information about all different types of food, such as fruits, vegetables, grains, gourds, and more: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4b1hwoq8oa866kchhddk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4b1hwoq8oa866kchhddk.png" alt="scientific foodie"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because I was building a database, I decided to have search and filter functionality so users could interact with the database to the fullest. &lt;/p&gt;

&lt;p&gt;In this app, my Search and Filter components work in tandem, so below, I walk through the steps of constructing each component and how I, and here's a hint, &lt;em&gt;chained&lt;/em&gt; them together. &lt;/p&gt;

&lt;p&gt;And since my Search and Filter components are siblings and children of my FoodContainer component, I determined that FoodContainer would hold state and handle the callback functions. Why? Because sibling components can't pass data to each other directly. Data can only flow up and down between parent and child. Therefore their parent component, FoodContainer, will hold state and handle the overall functionality.  &lt;/p&gt;




&lt;h1&gt;
  
  
  Making your Filter Component 📌
&lt;/h1&gt;

&lt;p&gt;Once you have the basic JSX written, i.e., what you're filtering by, it's time to construct the functionality. The value of your filter options should match the data key you're filtering. My app filters my food by group.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In your parent component, create state that will capture the user's selected filter. I like to set default state for my filter to "All," which is the first option in my Filter component. "All" ensures everything displays upon page load, and while filtering, the user can return to seeing everything at any point.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Next, we'll create a simple callback function to handle capturing the event.target.value, which is the filter change, and updating state. Initially, I always set my helper function to console log the selection, just so I know my callback is working — more on this in a minute.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1rhm9j0wd1f8dnozgcmp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1rhm9j0wd1f8dnozgcmp.png" alt="filter state and function"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pass the helper function as props from your parent component to your child, the Filter component. This allows the callback to be owned by a different component than the one invoking it. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Filter functionality takes an onChange event. Simply assign the helper function as props to onChange. Each time a filter option is selected, the value is passed up through our helper function to our parent component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0w77c7r1ocr8xja86ke.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0w77c7r1ocr8xja86ke.png" alt="filter component"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Try this out to make sure it works! How will you know? Remember, we have a temporary console log in the helper function in our parent component. In your app, select a filter option and see the value logged in your console. Again, &lt;em&gt;make sure your option values match your data&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4765ti78ivgwp2wgbgrf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4765ti78ivgwp2wgbgrf.png" alt="capturing filter in console"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now that you know the callback works remove the console log and replace it with your state's setter function. And add one more temporary console log to check the value of your state variable. In your app, select a filter again, and you'll see your state capturing the change in filters in the console! Finally, remove the console log.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1r5odjxwjjjqo3ul5al.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1r5odjxwjjjqo3ul5al.png" alt="updating state for filter"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Making your Search Component 🔎
&lt;/h1&gt;

&lt;p&gt;Leaving the Filter component aside, for now, let's construct our Search.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Set up state in the top-level parent component, in my case FoodContainer. The default value for your search state should be an empty string.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a simple callback function to handle what's typed in the search bar. As we initially did with our filter helper function, set your search helper function to console log the text in the search bar. We want to make sure it works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn935ukfg17dsw1v95py8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn935ukfg17dsw1v95py8.png" alt="search state and function"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, your Search component should take two props, the search state variable, and the helper function. We're passing state down as props because we're constructing a controlled component. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In your Search component, the value should take the search state variable as props, and your onChange takes the helper callback as props. Your helper is capturing the event.target.value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4dxmd3x7zw1i1uti1it4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4dxmd3x7zw1i1uti1it4.png" alt="state props"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjbpp97afhb1kx31jxymd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjbpp97afhb1kx31jxymd.png" alt="state component"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Try this out to make sure it works! With the temporary console log in our search helper function in our parent component, type something into your search bar. In the console, you'll see each keystroke logged as a single letter at a time. That's ok! We're not updating state just yet; we're only console logging to ensure the callback is working correctly. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Next, remove that console log and replace it with the search state's setter function. Then add one more temporary console log to check that your state is updating correctly and type again in the search bar. You'll see the text in the console &lt;em&gt;and&lt;/em&gt; the word forming in the search bar. Cool! Because this is a controlled component with state assigned as the search value, the letters appear in the search bar as you type, aka the user is watching state update in real-time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4fg527bh0zh3jvqocvat.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4fg527bh0zh3jvqocvat.png" alt="setting search state"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, remove that last console log.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  Chaining Filter and Search together ⛓
&lt;/h1&gt;

&lt;p&gt;The last step is updating the items displayed on the page based on what's being searched and filtered. In my case, I want my users to search for a specific food in the database based on the food's common name, filter by food group, &lt;em&gt;and&lt;/em&gt; do these actions together.&lt;/p&gt;

&lt;p&gt;My parent component FoodContainer handles displaying my food on the page, so that's where I added the following code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Declare the variable that holds the array you'll map over. I originally mapped my entire foods array, but that array didn't have my search and filter logic built in. So, I created a variable that handled my search and filter; this array will be mapped to display my items instead.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write an if statement. If the selected filter is "All," then all of the elements in the array will return. This is written as &lt;code&gt;return true&lt;/code&gt;. Otherwise, return the elements that match the user's selected filter.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We could stop here; however, we want search and filter to work &lt;em&gt;together&lt;/em&gt; so the user can utilize both. A couple more steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Next, &lt;strong&gt;chain on another filter&lt;/strong&gt;. Now our variable, &lt;code&gt;foodsToDisplay&lt;/code&gt;, takes both the filter and the search code into consideration instead of one or the other. Chaining methods is essentially calling methods one after another in one statement, allowing your code to be concise and the logic to continue. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In my case, I'm allowing users to search for foods by common name. &lt;code&gt;includes&lt;/code&gt; is added because I want to return partial matches as the user types. Finally, I'm converting the common name and the search to lowercase so capitalization doesn't get in the way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc46p3gnmd3mcugdvfdr6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc46p3gnmd3mcugdvfdr6.png" alt="filter and search together"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;TLDR  😎 - either the user searches through all the foods &lt;em&gt;or&lt;/em&gt; searches a filtered selection.&lt;/p&gt;

&lt;p&gt;Now, finally, let's map over the new array! Once done, try out search and filtering in your app. It's pretty cool.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4j3zjnq7nfma9g5on5dp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4j3zjnq7nfma9g5on5dp.png" alt="full code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So how did it go for me? Here's how my React project turned out:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqrz0svxn8mgdzuf2mjq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqrz0svxn8mgdzuf2mjq.gif" alt="gif"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Utilizing a Nested GET Request</title>
      <dc:creator>LaurenTyson🕶</dc:creator>
      <pubDate>Sun, 03 Jul 2022 21:57:07 +0000</pubDate>
      <link>https://forem.com/laurentyson85/utilizing-a-nested-get-request-3dj9</link>
      <guid>https://forem.com/laurentyson85/utilizing-a-nested-get-request-3dj9</guid>
      <description>&lt;p&gt;I'm a Flatiron Bootcamp student, and at the end of my first bootcamp phase, I was tasked with building a one-page web app with JavaScript, HTML, and CSS utilizing a pubic API. I decided to create a cocktail recipe generator from the API &lt;a href="https://www.thecocktaildb.com/api.php"&gt;The Cocktail DB&lt;/a&gt;, but the data I needed from my API was at two different urls; the second url being dependent on the first one. What to do 🤷‍♀️&lt;/p&gt;

&lt;p&gt;As a beginner, fetching data from a server can feel intimidating. Fetch requests default to GET requests which fortunately is pretty straightforward - you're sending a request via a URL to the server for data. The basic syntax for a GET request is something like this:&lt;/p&gt;

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

&lt;p&gt;But if you're like me and need data from two urls then that's done with a nested fetch. Let's walk through the process👇&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested fetch has entered the chat
&lt;/h2&gt;

&lt;p&gt;To start, dig into your API urls to see what data is accessible and how you'll be able to request it. When looking at The Cocktail DB's urls, I found I needed one url that returned a spirits array based on the user's spirit selection, &lt;em&gt;but&lt;/em&gt; that array didn't give me the complete ingredients for a specific cocktail. A different url returned the full recipe accessible via the drink ID. The drink ID was in my first url, which meant my second url was dependent on my first - 😅 whew!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;snippet of first API url:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--25ZoinjP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pthtmfalksg5p2nwoto.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--25ZoinjP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pthtmfalksg5p2nwoto.png" alt="snippet of first API url" width="880" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;snippet of second API url:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wVtNxAEt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvwa038815mimkoa7iqk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wVtNxAEt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvwa038815mimkoa7iqk.png" alt="snippet of second API url" width="880" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fortunately, the path of a url can be interpolated to dynamically get what we need based on an input. This is the starting point to building a nested fetch; the code that set's up your second fetch is inside your first fetch's second &lt;code&gt;.then&lt;/code&gt;. I'll show you in a code snippet below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding logic to your second &lt;code&gt;.then&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;My app generates a random cocktail based on the user's mood and spirit choices. Mood is just for fun; my app is primarily built on spirit selection. So for example, lets say my user selects vodka as their spirit. My first fetch makes a request to the server and returns an array of objects of all the vodka cocktails on the server. Each object in that array has a cocktail name, an image url, and drink ID. I need the drink ID for my &lt;em&gt;second&lt;/em&gt; fetch because my second url requests a cocktail by its ID to return the full recipe details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;url of my first fetch interpolating the user's spirit selection:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fnmhgkyL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83j2mnotpe8msh6p4vcx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fnmhgkyL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83j2mnotpe8msh6p4vcx.png" alt="url of my first fetch interpolating the user's spirit selection" width="880" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A cool thing about my app is that it generates a &lt;em&gt;different&lt;/em&gt; drink each time the user makes their selection. By utilizing Math.random I'm able to grab a different drink id from my first array every time my code block runs, &lt;em&gt;even if the user selects the same spirit multiple times&lt;/em&gt; 😲:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;selecting random drink id from my array after first fetch:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OusF4hbY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvs9kommy9yoywjlxjtw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OusF4hbY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvs9kommy9yoywjlxjtw.png" alt="selecting random drink id from my array after first fetch" width="880" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;To put it together, here are all the steps I took to build my nested fetch:&lt;/strong&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;My first fetch returns an array of drinks array based on a user's spirit selection&lt;/li&gt;
&lt;li&gt;I map over that drink array to return a new array of just the drink ids&lt;/li&gt;
&lt;li&gt;I select a random drink ID from that array by using Math.random&lt;/li&gt;
&lt;li&gt;But - I need the &lt;em&gt;value&lt;/em&gt; of that random drink ID, so I access that value using bracket notation&lt;/li&gt;
&lt;li&gt;That value is the server's drink ID that I need for my second GET request. I interpolate that value as the path of my second fetch url.&lt;/li&gt;
&lt;li&gt;My second fetch returns a complete recipe for a single cocktail.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;my total nested fetch code:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fi4kMDNy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r17xg8fxpkyw70dzfk4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fi4kMDNy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r17xg8fxpkyw70dzfk4.png" alt="total nested fetch code" width="880" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And if you want to refactor your nested fetch so it's a little more readable, you can break out your second fetch into it's own function, like this 👇&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;my total nested fetch code with second fetch as separate function:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U4c61mvf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhgxpdi3csslieqytrmo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U4c61mvf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhgxpdi3csslieqytrmo.png" alt="nested fetch with second fetch as callback" width="880" height="545"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;To build a nested fetch you don't need to randomize your results as I do with this app. You can do whatever you like with your data. Just know that your second fetch depends on the data you receive from your first fetch and utilizing interpolation allows the path of your fetch url to be dynamic.&lt;/p&gt;

&lt;p&gt;Once my full recipe returns from my second fetch, my app continues to run. The code in my last &lt;code&gt;.then&lt;/code&gt; invokes my functions that render my user's cocktail to the page.&lt;/p&gt;

&lt;p&gt;🍹&lt;/p&gt;

&lt;h2&gt;
  
  
  A couple of things to watch out for with nested fetches
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Watch your logic when interpolating the path of your second url. Can you actually request what you specifically want with this path? Test all the possible paths in your browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fetches are asynchronous code, so double-check the rest of your JavaScript outside of your fetches. If you don't want specific code to run until your second fetch returns, then put that code in a function(s) only to be invoked at the end of your second fetch. Like this:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--al2SXmUp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqextat0wgsc57rfy0s2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--al2SXmUp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqextat0wgsc57rfy0s2.png" alt="additional functions" width="710" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add console logs to your fetches to double-check that the data you're returning from the server is actually what you want. Remove those console logs once you know what's being returned is correct.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it! Nested fetches aren't that intimidating once you spend some time with your API to know where your data is and how to access it. Then walk through your code logically and check along the way to ensure you're returning what you expect.&lt;/p&gt;

&lt;p&gt;So how did it go for me? Here's how my project turned out:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CEInLeVr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlmdnhujmc76n3tkkfbz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CEInLeVr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlmdnhujmc76n3tkkfbz.gif" alt="Image description" width="720" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How to learn something really fast: the life of a bootcamp student</title>
      <dc:creator>LaurenTyson🕶</dc:creator>
      <pubDate>Sun, 19 Jun 2022 16:58:20 +0000</pubDate>
      <link>https://forem.com/laurentyson85/how-to-learn-something-really-fast-the-life-of-a-bootcamp-student-obc</link>
      <guid>https://forem.com/laurentyson85/how-to-learn-something-really-fast-the-life-of-a-bootcamp-student-obc</guid>
      <description>&lt;p&gt;Learning to code in a bootcamp is the hardest thing I've ever done.&lt;/p&gt;

&lt;p&gt;I'm a Flatiron bootcamp student. Since I work full-time and don't live near one of their campuses, I opted for their part-time online program, Flex. Graduation date: Feb 2023 🙌&lt;/p&gt;

&lt;p&gt;I've always considered myself a person who can learn anything, especially when I set my mind. As a kid, I taught myself how to paint and draw. As an adult, I taught myself data analytics &lt;em&gt;after&lt;/em&gt; I got a job as a data analyst. &lt;/p&gt;

&lt;p&gt;Honestly, Google can teach anybody anything. &lt;/p&gt;

&lt;p&gt;So when I decided to pivot my career to software development and joined a 10-month bootcamp knowing that it'd be fast-paced and challenging, I approached this challenge like I have others. Inner me was like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lp1eZip4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idx6o3ntkuugecbexmnh.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lp1eZip4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idx6o3ntkuugecbexmnh.jpeg" alt="Baby: I got this" width="547" height="557"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;But now, about six weeks into the program, I've realized a new truth - learning something new very quickly is &lt;em&gt;hard and uncomfortable.&lt;/em&gt; I wasn't at all prepared for those feelings.&lt;/p&gt;

&lt;p&gt;Typically when learning something new, like a language, cooking, or even knitting, you have time to review documentation, practice your developing skill as much as you like, and maybe take a day, or several, off from that skill to give yourself time to breathe. Over time your ability to quickly recall how to do that new thing becomes faster and better. &lt;/p&gt;

&lt;p&gt;What's different about learning to code in a fast-paced bootcamp is that the pacing is so quick that I don't have time to go back and review lessons or practice as much as I like. I don't have time to make myself more comfortable. Instead, I have to trust that the information is in my brain (waves around)...somewhere. The bootcamp experience feels like I'm in a constant state of motion, running on swaying ground. I never feel solidly grounded.&lt;/p&gt;

&lt;p&gt;So when I say learning to code in a bootcamp is the hardest thing I've ever done, it's not necessarily the concepts I'm learning that are hard; it's how fast I have to learn them.&lt;/p&gt;

&lt;p&gt;Now, I'd still recommend learning to code in a bootcamp like Flatiron. The lessons are solid, the teaching method is practical, and my engineering friends tell me that I'll come out of the program probably knowing more than them (haha, awesome). &lt;/p&gt;




&lt;h2&gt;
  
  
  Tips for learning how to code really quickly👇
&lt;/h2&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code every day&lt;/strong&gt;. Seriously Every. Day. Even if all you have is 30-60 minutes to work through your lessons. Repetition is essential, especially when learning something technical. Coding every day will help reinforce those developing brain pathways.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ask for help when you're stuck&lt;/strong&gt;. Whether it's asking Google, another bootcamp student, or your teacher. When learning fast, you don't have time to wallow too long in "figuring it out on your own" or feel shy or defeated for asking for help. It's best to keep learning and moving through the material. So when I'm stuck getting my tests to pass a lab or when a new concept is challenging to wrap my head around, I give myself about 30 minutes to figure it out on my own before seeking help. The help nudges me in the right direction then I can figure the rest out. It's also a confidence boost because sitting alone at my desk with no movement is defeating. Reach out to others when you need it so you can keep going.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Make flash cards&lt;/strong&gt;. A tried and true method, I love flashcards. Why? Because as I said before, repetition is key to learning something quickly. I keep my flashcards with me and work through them throughout the day while cooking, cleaning, and working my full-time job.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Evaluate your priorities&lt;/strong&gt;. Once I realized how uncomfortable learning to code this quickly would be, I realized I needed to prioritize the bootcamp and deprioritize other activities because there's only so much time in the day. I made a list of what I was doing with my time outside the bootcamp and work and deprioritized things like TV, podcasts, and sleeping in so I could spend more time coding. It was hard to drop some of my favorite activities, but it's only temporary while I'm focused on learning code. Then I communicated my new focus and priorities to my husband and friends, so everyone is on the same page.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Confidence and self-love&lt;/strong&gt;. I have to tell myself out loud daily that everything will be ok. And I've been working on believing that I know more than I do. It's hard, but filling your head with constant positive reinforcement will help you tremendously to have the confidence to keep going. Here is one of my favorite Ted Talks on self-confidence and the importance of repetition; check it out! &lt;a href="https://www.youtube.com/watch?v=w-HYZv6HzAs"&gt;The skill of self-confidence | Dr. Ivan Joseph | TEDxRyersonU&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/w-HYZv6HzAs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Alright, that's it. Get out there and learn something new. With repetition and some self-love, you'll be able to do it even if the pace is fast. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>productivity</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>JavaScript typeof null likes to play tricks</title>
      <dc:creator>LaurenTyson🕶</dc:creator>
      <pubDate>Sat, 21 May 2022 14:02:04 +0000</pubDate>
      <link>https://forem.com/laurentyson85/javascript-typeof-null-likes-to-play-tricks-41oa</link>
      <guid>https://forem.com/laurentyson85/javascript-typeof-null-likes-to-play-tricks-41oa</guid>
      <description>&lt;p&gt;As a newbie coder, everything is confusing. I'm always like, wait - what? That was precisely my thought when I asked JavaScript &lt;code&gt;typeof&lt;/code&gt; &lt;code&gt;null&lt;/code&gt;, and it told me &lt;code&gt;object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PCRai8HB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4vmqk5cebktju22bkb1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PCRai8HB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4vmqk5cebktju22bkb1.png" alt="shows typeof examples in node" width="880" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Huh? &lt;/p&gt;

&lt;p&gt;If null is an object, and objects are a collection of properties, that would mean null has some value. But we &lt;em&gt;know&lt;/em&gt; that's not true; therefore, this is JavaScript playing a trick on us. Let's break down why.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;Null&lt;/code&gt; isn't an object.
&lt;/h1&gt;



&lt;p&gt;&lt;code&gt;Null&lt;/code&gt; is just, &lt;code&gt;null&lt;/code&gt;. In JavaScript, &lt;code&gt;null&lt;/code&gt; is a structured root primitive value. It stands by itself. It's the &lt;em&gt;absence&lt;/em&gt; of value, unlike &lt;code&gt;undefined&lt;/code&gt; which is an &lt;em&gt;empty&lt;/em&gt; value—two different things.&lt;/p&gt;

&lt;p&gt;Then why does JavaScript return object? Because &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof"&gt;according to MDN&lt;/a&gt;, JavaScript values are represented as a type tag and a value, and the type tag for objects is 0 thus lumping &lt;code&gt;null&lt;/code&gt; in as an object. &lt;/p&gt;

&lt;p&gt;As we can imagine, representing the &lt;em&gt;absence of value&lt;/em&gt; is tricky; as an aside, in other programming languages, null is represented as the null pointer 0x00. &lt;a href="https://softwareengineering.stackexchange.com/questions/128327/what-is-the-difference-between-a-pointer-pointing-to-0x0-location-and-a-pointer"&gt;Reference&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In an effort to fix this bug, in 2015, when JavaScript went through its rework it was proposed to change to &lt;code&gt;typeof null === null&lt;/code&gt;. Unfortunately, that fix was rejected, but honestly, for a good reason. ES Wiki, the web archive for the ECMAScript programming language, says:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"This proposal has been rejected. It was implemented in V8 but it turned out that it broke a lot of existing sites. In the spirit of One JavaScript this is not feasible."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://web.archive.org/web/20160331031419/http://wiki.ecmascript.org:80/doku.php?id=harmony:typeof_null"&gt;Check out the ES Wiki page I'm referencing, the full explanation is fascinating.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since developers have always known of the  &lt;code&gt;typeof&lt;/code&gt; &lt;code&gt;null&lt;/code&gt; bug, many people wrote their code to account for it. And while the proposed fix would have fixed our brains, it would have broken our apps.&lt;/p&gt;

&lt;p&gt;So now we newbies know of this bug too! Don't let this break your brain as it did mine. It's just one of the many fascinating oddities of JavaScript programming.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
