<?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: Olivia Pomeroy</title>
    <description>The latest articles on Forem by Olivia Pomeroy (@opomeroy26).</description>
    <link>https://forem.com/opomeroy26</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%2F815315%2F32aa2182-0feb-4c83-b5b4-679fe06a7fb4.jpeg</url>
      <title>Forem: Olivia Pomeroy</title>
      <link>https://forem.com/opomeroy26</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/opomeroy26"/>
    <language>en</language>
    <item>
      <title>Learning to Learn How to Code</title>
      <dc:creator>Olivia Pomeroy</dc:creator>
      <pubDate>Wed, 20 Apr 2022 19:30:25 +0000</pubDate>
      <link>https://forem.com/opomeroy26/learning-to-learn-how-to-code-2l5o</link>
      <guid>https://forem.com/opomeroy26/learning-to-learn-how-to-code-2l5o</guid>
      <description>&lt;h2&gt;
  
  
  How to Learn
&lt;/h2&gt;

&lt;p&gt;I started a software engineering bootcamp about 2 months ago, and while I wasn't expecting it to be easy, I don't think I fully realized what parts would be the most challenging. One of those for me was re-learning how to learn. I had been out of school for a couple years before beginning the program so when I went back into a learning environment, I wasn't really sure what best practice would be- especially with a topic I had no previous experience in. This are some tips that I found helpful while re-navigating how to learn. &lt;/p&gt;

&lt;h2&gt;
  
  
  Learning Methods
&lt;/h2&gt;

&lt;p&gt;There are three main learning methods that people learn by: watching, listening, and doing it themselves. There may be one that people gravitate more towards, however it seems the best practice is to do a combination of the three. Watching youtube videos and tutorials was something that helped me through, and then creating "playgrounds" in my terminal or messing around in REPL to try to replicate what I had just seen or read. With coding in particular, the most beneficial learning method I found was to do it myself. That way if I get stuck, you learn how to get good at googling, how to ask the right questions, and how to find the answers. &lt;/p&gt;

&lt;h2&gt;
  
  
  Practice
&lt;/h2&gt;

&lt;p&gt;Practice! Practice! Practice! Continuous practice helps strengthen our memory and can get us used to thinking in specific ways, rather than just memorizing code. By practicing, we are constantly learning new material as we go through, making it easier each time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Yourself
&lt;/h2&gt;

&lt;p&gt;Testing yourself is a great way to see what you have learned and what you still need to develop. One of the hardest things with learning, is feeling like you're not doing it well. But that's ok- because it's just learning! We need to put away the thoughts that we have to be perfect or that we can't make mistakes, because we're going to (a lot), and those mistakes are what's going to help us out in the long run because we can target our skillset.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write It Out
&lt;/h2&gt;

&lt;p&gt;The reason why practice is so good is because it is repetition and get's our brains used to looking at similar information. Writing things out by hand is known to also have a longer-lasting effect in our memory. It sounds weird for coding to grab a pen and paper, but something I found helpful was to write out flashcards when I was first starting off with the new terminology and any specific syntax that I had trouble with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Speak it Out Loud
&lt;/h2&gt;

&lt;p&gt;Similarly to writing it out, speaking it will also help store it in your memory better than just reading about it. In addition, it also is proven that it's helpful to try to relate the topic to something you have already learned or know. You can learn in building blocks. You can relate it to something similar (like relating how Ruby methods are like JavaScript functions), or you can relate topics to metaphors within your own life. Making things personal will also help better store it within your memory. &lt;/p&gt;

&lt;h2&gt;
  
  
  Teach
&lt;/h2&gt;

&lt;p&gt;When learning something new, although maybe anti-intuitive, it's very helpful to then try teaching that concept to another person. If you have classmates, try to answer their questions when you can. Or challenge yourself to see if you could explain some concepts to a younger child. It will help you solidify any understand you have, and it may help you hone in on what you don't. &lt;/p&gt;

&lt;h2&gt;
  
  
  Primacy vs Recency
&lt;/h2&gt;

&lt;p&gt;Primacy effect is when you better recall information you learned first and recency effect is when you better recall information you learned last. Both are shown to better store information into long term memory than what was learned in the middle. We can incorporate both of these while we study by starting with concepts that are trickier for us, and then ending by reviewing those topics again. &lt;/p&gt;

&lt;h2&gt;
  
  
  Keep Learning
&lt;/h2&gt;

&lt;p&gt;Learning is something that can be practiced and the more opportunities we take to learn the easier it will be because our mind will already be used to that state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Be Kind to Yourself
&lt;/h2&gt;

&lt;p&gt;It's okay to make mistakes. That's how you learn. It's important to be kind to yourself, your brain, and your body. Getting a good nights sleep can help improve our memory. Staying hydrated as well. As much as we want to cram a night before, it's not the most productive thing we can do to actually learn and solidify information. And take plenty of breaks!&lt;/p&gt;

&lt;h2&gt;
  
  
  Have Some Fun With It
&lt;/h2&gt;

&lt;p&gt;You're going to dread learning if it's not something you're interested in or you're making the information more boring than it may need to be. &lt;a href="https://www.computerworld.com/article/2947706/how-programmers-remember-all-that-programming-stuff.html"&gt;Here&lt;/a&gt; is an article with some mnemonic devices people have used to help them remember topics in software engineering. Some examples are &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;'I never remember that . = class and # = id in CSS, so I use ‘say no to drugs’ to remember that ‘hash is not class’.” hill79&lt;/p&gt;

&lt;p&gt;“.unshift - makes it bigger. Longer word than .shift&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;.shift - makes it smaller. Shorter word than .unshift” da02&lt;/p&gt;

&lt;h2&gt;
  
  
  Happy Learning!
&lt;/h2&gt;

&lt;p&gt;Sources&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.verywellmind.com/understanding-the-primacy-effect-4685243#:%7E:text=The%20primacy%20effect%20involves%20rehearsing,items%20in%20short%2Dterm%20memory"&gt;https://www.verywellmind.com/understanding-the-primacy-effect-4685243#:~:text=The%20primacy%20effect%20involves%20rehearsing,items%20in%20short%2Dterm%20memory&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.computerworld.com/article/2947706/how-programmers-remember-all-that-programming-stuff.html"&gt;https://www.computerworld.com/article/2947706/how-programmers-remember-all-that-programming-stuff.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.entrepreneur.com/article/323450"&gt;https://www.entrepreneur.com/article/323450&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.verywellmind.com/how-to-become-a-more-effective-learner-2795162"&gt;https://www.verywellmind.com/how-to-become-a-more-effective-learner-2795162&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://zapier.com/blog/learning-new-skills/"&gt;https://zapier.com/blog/learning-new-skills/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Ruby on Rails: Validation Errors</title>
      <dc:creator>Olivia Pomeroy</dc:creator>
      <pubDate>Wed, 06 Apr 2022 16:27:27 +0000</pubDate>
      <link>https://forem.com/opomeroy26/ruby-on-rails-validation-errors-3d3b</link>
      <guid>https://forem.com/opomeroy26/ruby-on-rails-validation-errors-3d3b</guid>
      <description>&lt;h2&gt;
  
  
  Validation
&lt;/h2&gt;

&lt;p&gt;Validations are used in Rails to ensure the data saved to the database is valid. It helps prevent any issues further further down the road. Such as if a user were to fill out a form, it's important they fill it out with valid data- like confirming their phone number is 10 digits long. This will save a lot of debugging and headaches, preventing that issue to persist until it's used.&lt;/p&gt;

&lt;p&gt;There are a couple ways to check if the object is valid. In rails, validations are run before &lt;em&gt;saving&lt;/em&gt; an active record object, and if there are errors then it will not save. If a user tries to input a phone number with 9 digits, the validation will recognize it is not valid and won't save the information in the data base, giving the user an error message.&lt;/p&gt;

&lt;p&gt;We can use valid? or invalid? to check this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
   &lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;presence: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt; &lt;span class="n"&gt;minimum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can open an irb/console session and test out the validity of our Person class, by saving a new instance method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; Person.create(phone: 555-321-112).invalid?
=&amp;gt; true
irb&amp;gt; Person.create(phone: 555-321-112).valid?
=&amp;gt; false
irb&amp;gt; Person.create(phone: 555-321-1128).valid?
=&amp;gt; true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;valid?&lt;/em&gt; will trigger the validations and return true if no errors were found and false if there are errors. &lt;em&gt;invalid?&lt;/em&gt; is the opposite and will return true if there are errors and false if there aren't errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Validation Errors
&lt;/h2&gt;

&lt;p&gt;We can run our own validations while writing our code to check the validity as we work. After Active Record completes the validations, any errors that may arise can be looked at through errors instance methods, which will return the errors. In order for an object to be valid, the collection of errors must be empty after running the validation. We'll go through errors collection to help us do this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors Collection
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;errors&lt;/strong&gt; : errors returns an instance of the class that contains the errors. It allows us to further look into the different details of the error, so we can attach other methods to the end of it. Some common methods to pair with errors is &lt;em&gt;.message&lt;/em&gt;, .&lt;em&gt;full_messages&lt;/em&gt; (&lt;em&gt;to_a&lt;/em&gt; alias), and &lt;em&gt;.full_messages_for(:attribute)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; person = Person.new
irb&amp;gt; person.valid?
=&amp;gt; false
irb&amp;gt; person.errors.message
=&amp;gt; "is too short(minimum is 10 characters)"
irb&amp;gt; person.errors.full_messages
=&amp;gt; ["Phone is too short (minimum is 10 characters)"]
irb&amp;gt; person.errors.full_messages_for(:phone)
=&amp;gt; ["is too short (minimum is 10 characters)"]

irb&amp;gt; person = Person.new(phone: 555-643-2342)
irb&amp;gt; person.valid?
=&amp;gt; true
irb&amp;gt; person.errors.full_messages
=&amp;gt; []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;errors[]&lt;/strong&gt; : To verify a particular attribute of the object we can run &lt;em&gt;errors[:attribute]&lt;/em&gt;. It will return an array of error message strings for that particular attribute, and if there are no errors it will return empty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; person = Person.new(phone: 555-123-123)
irb&amp;gt; person.valid?
=&amp;gt; false
irb&amp;gt; person.errors[:phone]
=&amp;gt; ["is too short (minimum is 10 characters)"]

irb&amp;gt; person = Person.new(phone: 555-123-1234)
irb&amp;gt; person.valid?
=&amp;gt; true
irb&amp;gt; person.errors[:name]
=&amp;gt;[]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;errors.where&lt;/strong&gt; : returns an array of error objects. We use this compared to errors[] when we want more information than just the message. From the objects, you can then look further into it to get more information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; person = Person.new
irb&amp;gt; person.valid?
=&amp;gt;false

irb&amp;gt; person.errors.where(:phone)
=&amp;gt;[...] #all errors for :phone attribute. Both presence and length errors

irb&amp;gt; person.errors.where(:phone, :too_short) 
=&amp;gt; [...] # :too_short errors for :phone attribute

irb&amp;gt; person.errors.where(:phone).first.attribute
=&amp;gt; :name
irb&amp;gt; person.errors.where(:phone).first.type
=&amp;gt; :too_short
irb&amp;gt; person.errors.where(:phone).first.options[:count]
=&amp;gt; 10

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;errors.add&lt;/strong&gt; : creates the error object by taking an attribute, an error type and additional options hash. These are helpful if you are creating your own validators&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;validate&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
     &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="ss"&gt;:phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:too_long&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;message: &lt;/span&gt;&lt;span class="s2"&gt;"has too many numbers"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; person = Person.create
irb&amp;gt; person.errors.where(:phone).first.type
=&amp;gt; :too_long
irb&amp;gt; person.errors.where(:phone).first.full_message
=&amp;gt; "Phone has too many numbers

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;errors[:base]&lt;/strong&gt; : can add errors to the objects state as a whole instead of being related to a specific attribute as with &lt;em&gt;errors.add&lt;/em&gt;. Also good for when you are writing your own validations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;validate&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="ss"&gt;:base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:invalid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;message: &lt;/span&gt;&lt;span class="s2"&gt;"This phone number is invalid because ..."&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; person = Person.create
irb&amp;gt; person.errors.where(:base).first.full_message
=&amp;gt; "This phone number is invalid because ..."

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;errors.clear&lt;/strong&gt; : used when you intentionally want to clear the errors collection. It doesn't actually make the object valid though, the errors collection will be empty but next time you save or validate that method, the validations will run again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; person = Person.new
irb&amp;gt; person.valid?
=&amp;gt; false
irb&amp;gt; person.errors.empty?
=&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; person.errors.clear
irb&amp;gt; person.errors.empty?
=&amp;gt; true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;errors.size&lt;/strong&gt; : returns the total number of errors for the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; person = Person.new
irb&amp;gt; person.valid?
=&amp;gt; false
irb&amp;gt; person.errors.size
=&amp;gt; 2 #phone does not exist, and is less than minimum length.

irb&amp;gt; person = Person.new(phone: 555-123-123)
irb&amp;gt; person.valid?
=&amp;gt; false
irb&amp;gt; person.errors.size
=&amp;gt; 1 # phone exists, but below minimum length.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Errors in relation to custom methods
&lt;/h2&gt;

&lt;p&gt;We can add errors to our collection when they are invalid. We can conditionally render out errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;validate&lt;/span&gt; &lt;span class="ss"&gt;:phone_cannot_be_more_than_number&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;phone_cannot_be_more_than_number&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;present?&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;
  &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"number is too long"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When added to custom methods, it will run every time you call valid? or save the object, unless we give it an :on option to validate the method with either :create or :update.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/active_record_validations.html#custom-methods"&gt;https://guides.rubyonrails.org/active_record_validations.html#custom-methods&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://api.rubyonrails.org/v6.1.3/classes/ActiveModel/Errors.html#method-i-messages"&gt;https://api.rubyonrails.org/v6.1.3/classes/ActiveModel/Errors.html#method-i-messages&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/active_record_validations.html#working-with-validation-errors"&gt;https://guides.rubyonrails.org/active_record_validations.html#working-with-validation-errors&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>programming</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Active Record Associations Guide</title>
      <dc:creator>Olivia Pomeroy</dc:creator>
      <pubDate>Tue, 22 Mar 2022 23:53:24 +0000</pubDate>
      <link>https://forem.com/opomeroy26/active-record-associations-guide-5h56</link>
      <guid>https://forem.com/opomeroy26/active-record-associations-guide-5h56</guid>
      <description>&lt;h2&gt;
  
  
  Active Record Associations
&lt;/h2&gt;

&lt;p&gt;Programmers use associations in code to be able to access data in different tables. Associations allow us to shorten down our code and makes things simpler but helping us not write as much SQL. Associations follow the idea of "convention over configuration", which is the most ideal practice when it comes to programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Active Record Associations
&lt;/h2&gt;

&lt;p&gt;There can be different types of relationships between Classes. We're going to go through the following: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one-to-one&lt;/li&gt;
&lt;li&gt;one-to-many&lt;/li&gt;
&lt;li&gt;many-to-many&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Our Example
&lt;/h2&gt;

&lt;p&gt;To be consistent throughout this blog, we're going to stick with one example and create different associations through that. In our example, we are going to have a Cookbook, which has an author, a publisher, recipes, and ingredients. We'll walk through the different relationships each of these may have with one another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to Begin
&lt;/h2&gt;

&lt;p&gt;The best place to start is by mapping out what data we have and understanding how our different tables will relate to each other. This helps us understand what kind of relationship the Classes have with each other, and will especially help us figure out where the foreign keys belong. The best way to do this is by creating an Entity Relationship Diagram (ERD).&lt;/p&gt;

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

&lt;p&gt;This ERD shows us our different tables, which columns that may have, and a rough idea as to the relationship amongst by tracking which tables have other tables foreign keys. This table may make a bit more sense as we walk through each relationship so take a look back at it along the way.&lt;/p&gt;

&lt;p&gt;Now we can start building out our associations in code by using Active Record macros (a method that helps write our code).&lt;/p&gt;

&lt;h2&gt;
  
  
  One-to-One Relationship
&lt;/h2&gt;

&lt;p&gt;A one-to-one relationship means that one table belongs to solely another. In our example, we can say that the cookbook has one publisher, and that publisher belongs to the cookbook. We write this using the &lt;strong&gt;has_one&lt;/strong&gt; macro along with the &lt;strong&gt;belongs_to&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cookbook&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;has_one&lt;/span&gt; &lt;span class="ss"&gt;:publisher&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Publisher&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:cookbook&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Syntax Notes&lt;/em&gt;: &lt;strong&gt;has_one&lt;/strong&gt; and &lt;strong&gt;belongs_to&lt;/strong&gt; both will point to a word in &lt;em&gt;singular&lt;/em&gt; form.&lt;/p&gt;




&lt;p&gt;This establishes a &lt;em&gt;one-to-one, bi-directional&lt;/em&gt; connection since they point to each other.&lt;/p&gt;

&lt;p&gt;We can establish a &lt;em&gt;one-to-one, one-directional&lt;/em&gt; connection by using &lt;strong&gt;belongs_to&lt;/strong&gt; on it's own. Such as, an editor may belong to a publisher, but the publisher may not always have or use the editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Editor&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:publisher&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can create another &lt;em&gt;one-to-one, bi-directional&lt;/em&gt; connection by using a join table. In this case we would use the macro &lt;strong&gt;has_one through:&lt;/strong&gt;. We have an author, that has a publisher, who publishes their cookbook. Therefore, the cookbook has an author. The publisher belongs both to the cookbook and the author, as a "middle-man". We would write this like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cookbook&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;has_one&lt;/span&gt; &lt;span class="ss"&gt;:publisher&lt;/span&gt;
  &lt;span class="n"&gt;has_one&lt;/span&gt; &lt;span class="ss"&gt;:author&lt;/span&gt; &lt;span class="ss"&gt;through: :publisher&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Publisher&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:cookbook&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:author&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Author&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;has_one&lt;/span&gt; &lt;span class="ss"&gt;:publisher&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;: The order of macros matters! We need to put our has_one :car, before going through it in our Owner class.&lt;/p&gt;

&lt;h2&gt;
  
  
  One-to-Many Relationship
&lt;/h2&gt;

&lt;p&gt;A one-to-many relationship means that one class has many instances of another class, and that second class will belong to the first class. Our cookbook has many ingredients in it, and those ingredients belong to the cookbook. We can write this out using the &lt;strong&gt;has_many&lt;/strong&gt; macro, along with the &lt;strong&gt;belongs_to&lt;/strong&gt; macro.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cookbook&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
   &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:ingredients&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Ingredient&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
   &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:cookbook&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Syntax Notes&lt;/em&gt;: &lt;strong&gt;has_many&lt;/strong&gt; will always point to a word in &lt;em&gt;plural&lt;/em&gt; form. &lt;/p&gt;




&lt;p&gt;If you ever get confused, it's helpful to say aloud what it would sound like, as Ruby is very programmer friendly and makes it sound like our day to day grammar.&lt;/p&gt;




&lt;h2&gt;
  
  
  Many-to-Many Relationship
&lt;/h2&gt;

&lt;p&gt;Many-to-many means one table can have many instances of another, and visa versa. We've established our cookbook has many ingredients. Those ingredients make up (belong to)a recipe. We can then say our cookbook has many recipes, through the ingredients it has listed.  We would write this using the &lt;strong&gt;has_many through&lt;/strong&gt; macro.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cookbook&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
   &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:ingredients&lt;/span&gt;
   &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:recipes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;through: :ingredients&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Ingredient&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:cookbook&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:recipe&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Recipe&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
   &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:ingredients&lt;/span&gt;
   &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:cookbook&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the Ingredients class is a join table, because it joins the relationship between the two others, by allowing Cookbook to go through it to access Recipes.&lt;/p&gt;

&lt;p&gt;We can also use the macro &lt;strong&gt;has_and_belongs_to_many&lt;/strong&gt; which creates a direct many-to-many connection with another model, &lt;em&gt;without&lt;/em&gt; the joined table. We could say we have a recipe that combines two ingredients, to then use as an ingredient in another recipe (think making your own pasta sauce). The recipe has many ingredients, the ingredients have many recipes, the recipe belongs to the ingredients, and the ingredients also belong to the recipes. (Ik ik, it's a lot). This is how we would write it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Ingredient&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;has_and_belongs_to_many&lt;/span&gt; &lt;span class="ss"&gt;:recipes&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Recipe&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;has_and_belongs_to_many&lt;/span&gt; &lt;span class="ss"&gt;:ingredients&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Of note, this does not follow the relationship set up with our ERD since there will be a change to the foreign keys. In this situation, another join or junction table will be created to hold both foreign keys. The standard is to call the join table "recipes_ingredients".&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Associations during Migration
&lt;/h2&gt;

&lt;p&gt;We have been creating our associations through macros in our classes. We can also create associations when we make our tables before migration, which will help assist us with setting up the foreign keys. For example with our Cookbook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CreateANewCookbook&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;6.1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="ss"&gt;:cookbook&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="ss"&gt;:ingredient&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:cookbook&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:recipe&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;recipe&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:item_name&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Polymorphic Associations
&lt;/h2&gt;

&lt;p&gt;Polymorphic associations are when a model can belong to multiple other models, but with only a single association and doesn't go through a join table. Let's say we have food pictures. Those pictures could either belong to a recipe or it can belong to the cookbook (spread throughout not connected to the recipe). These two locations aren't connected to each other, and the pictures have a single association with each.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Picture&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:imageable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;polymorphic: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cookbook&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:pictures&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :imageable&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Recipe&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:pictures&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :imageable&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*imageable is an unwritten naming-convention. Imageable means that the Picture model doesn't belong to either Cookbook or Recipe, but belongs to imageable, which can then be used to create the association between either Cookbook and Picture or Recipe and Picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  To Conclude
&lt;/h2&gt;

&lt;p&gt;When we establish these relationships, it allows us then to use various methods on the classes. If we create a relationship between Ingredient and Cookbook, we can then call self(Cookbook).ingredient as a method, and so on and so forth. These Active Record Associations act as our getter and setter methods we may have used previously, so we no longer need to initiate or read the instance, because this does it for us. &lt;/p&gt;

&lt;p&gt;You know have your &lt;em&gt;associate&lt;/em&gt; degree - Happy coding!&lt;/p&gt;




&lt;p&gt;Sources&lt;/p&gt;

&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/association_basics.html"&gt;https://guides.rubyonrails.org/association_basics.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/59173076/correct-foreign-keys-for-has-and-belongs-to-many-association"&gt;https://stackoverflow.com/questions/59173076/correct-foreign-keys-for-has-and-belongs-to-many-association&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pilanites.com/polymorphic-associations-rails/"&gt;https://www.pilanites.com/polymorphic-associations-rails/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding Inverse Data Flow</title>
      <dc:creator>Olivia Pomeroy</dc:creator>
      <pubDate>Wed, 09 Mar 2022 04:06:47 +0000</pubDate>
      <link>https://forem.com/opomeroy26/understanding-inverse-data-flow-3pgb</link>
      <guid>https://forem.com/opomeroy26/understanding-inverse-data-flow-3pgb</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This blog requires some base knowledge of Props and State in React. You can read up on both &lt;a href="https://www.freecodecamp.org/news/react-js-for-beginners-props-state-explained/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is Inverse Data Flow?
&lt;/h2&gt;

&lt;p&gt;Up until this point, we have learned that the way data flows is from a parent component to a child component in the form of a prop. With inverse data flow, we are switching that flow so the data is passed up from the child component to the parent component. &lt;/p&gt;

&lt;h2&gt;
  
  
  How Does It Work?
&lt;/h2&gt;

&lt;p&gt;In order for a child component to pass up data up, the parent &lt;strong&gt;must pass down a callback function to the child&lt;/strong&gt;. The callback function will be passed down as a prop to the child (like in regular data flow). By having the function defined in the parent component, we can invoke it in the child and then the parent will have access to the passed data. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conceptualization
&lt;/h2&gt;

&lt;p&gt;This is a pretty important concept in React and something that you can use all the time. Once you get the hang of it, you can use it for component reusability, cleaner code, rendering components and help with your events. Before we jump into actually writing out code to show how it works, let's break it down to a very simplistic concept, using a situation many may be familiar with outside of code. Take a look at this diagram and below we will walk through how it helps explain inverse data flow. &lt;/p&gt;

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

&lt;p&gt;We have our parent (mom) and two of her children (Bob and Sally). Mom gives Bobby some toys (our state data) and Bobby now has access to those toys. Bob's sister wants access to toys as well so she asks Bob. Bob says no- this is important to note because &lt;strong&gt;Children cannot pass data to another child component&lt;/strong&gt;. It must pass through the parent first and then to the child. Sally asks mom for some toys and mom goes to Bob, who has all the toys(data). She puts out her hand (callback function) and Bob hands her a toy(invoking callback and passing data back up to parent). Mom now has possession of the toy, and she could hold it and do something else with it, but instead she passes it back down to Sally (setting a new state). To put it into a hierarchical structure, it'll look like this: &lt;/p&gt;

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

&lt;h2&gt;
  
  
  Seeing it in Code
&lt;/h2&gt;

&lt;p&gt;Now with that visualization of how data is passed, let's walk through an example of code. &lt;/p&gt;

&lt;p&gt;We are starting with an App component (our parent), which has GroceryItemsList and GroceryCart as children components. We are keeping State in our App component (an array of groceryItems)and passing it down to GroceryItemsList, and then iterating over it so it creates a list of individual grocery items(an object). Our goal is to be able to click on a GroceryItem and have it added to our GroceryCart component. This is visualization of our data flow: &lt;/p&gt;

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

&lt;p&gt;Let's start by making a callback function in our App component and passing that as a prop to our GroceryItemsList.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onGroceryItemClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//this is our callback function&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We are adding this to GroceryCart&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//making sure it passes it down and back up smoothly&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GroceryItemsList&lt;/span&gt; 
            &lt;span class="na"&gt;onGroceryItemClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onGroceryItemClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;//we are passing our callback function as a prop&lt;/span&gt;
            &lt;span class="na"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;groceries&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; 
        &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GroceryCart&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to go into our GroceryItemsList and destructure our prop, and pass it along to our GroceryItem. Of note: we are passing along our onGroceryItemClick callback function as a prop but naming it handleGroceryItemClick to make it clearer what it will be doing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;GroceryItemsList&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;onGroceryItemClick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;groceryItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;groceryObj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;GroceryItemCard&lt;/span&gt;
         &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;groceryObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
         &lt;span class="nx"&gt;groceryItem&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;groceryObj&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
         &lt;span class="nx"&gt;handleGroceryItemClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onGroceryItemClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;//passing callback down another child level&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
       &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;groceryItem&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in our GroceryItemCard, we are going to take that callback prop and give it the data it needs to invoke it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;GroceryItemCard&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;groceryItem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleGroceryItemClick&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;groceryItem&lt;/span&gt;

   &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;handleGroceryItemClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;groceryItem&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;//This says 'when we click on the GroceryItemCard we are going to invoke our function and send it a groceryItem (an object)&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h5&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h5&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now see that when we click on a GroceryItemCard it is console.logging:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;We are adding this to GroceryCart
 {id:1, item:'Milk', price:'$3.99', brand:'Lucerne', image:'https://media.istockphoto.com/photos/kefir-milk-or-turkish-ayran-drink-are-poured-into-a-glass-cup-from-a-picture-id1198789194?b=1&amp;amp;k=20&amp;amp;m=1198789194&amp;amp;s=170667a&amp;amp;w=0&amp;amp;h=f85XhCzCWQhdFn0LaRckyOkJui88sYNoctN9zEmS4y0=' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have successfully passed down a callback function and invoked it from child to parent! That's inverse data flow!!&lt;/p&gt;

&lt;p&gt;Now that the parent has the GroceryItem object, it can set the state of the GroceryCart to that object and render it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onGroceryItemClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//this is our callback function&lt;/span&gt;
 &lt;span class="nx"&gt;setGroceryCart&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;//Adding item to our GroceryCart by setting its state&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;GroceryItemsList&lt;/span&gt; 
            &lt;span class="nx"&gt;onGroceryItemClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onGroceryItemClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
            &lt;span class="nx"&gt;groceries&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
        &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;GroceryCart&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;And there we go- Inverse Data Flow. In my experience inverse data flow works really well for events (onChange, onClick, etc (especially search bars or any kind of filters)). It helps with component reusability and organizing where we put State in our library. It is a tricky concept to understand, but what's helped me is to make sure you name props appropriately and work from top to bottom, from parent to child, so you can keep track of what is being passed down. &lt;/p&gt;




&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://reactjs.org/docs/thinking-in-react.html"&gt;https://reactjs.org/docs/thinking-in-react.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>react</category>
      <category>beginners</category>
      <category>simplify</category>
    </item>
    <item>
      <title>How to Debug in JavaScript</title>
      <dc:creator>Olivia Pomeroy</dc:creator>
      <pubDate>Tue, 15 Feb 2022 21:10:12 +0000</pubDate>
      <link>https://forem.com/opomeroy26/how-to-debug-in-javascript-4edg</link>
      <guid>https://forem.com/opomeroy26/how-to-debug-in-javascript-4edg</guid>
      <description>&lt;h2&gt;
  
  
  How To Debug In JavaScript
&lt;/h2&gt;

&lt;p&gt;Unlike the systems we use- we are human! Which means we are going to make mistakes, and probably a lot of them. There is nothing worse than writing a giant chunk of code, just to realize it isn't working the way you hoped. Is it a syntax error? Did you forget to call the function? Thankfully there are some tools we can use to help us figure out the issue so we can continue creating some really cool stuff! In this post we're going to understand what debugging is and look into two options on how to debug in JavaScript. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The browser I'll be using is Chrome, but most other browsers have a similar process.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is Debugging?
&lt;/h2&gt;

&lt;p&gt;Debugging is a tool developers use in order to go through their code and fix any errors that may arise. It can be done by examining a line of code to ensure it runs the way we'd like, or by forcing the code to pause at a certain line, letting us inspect the set block of code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Console.log()
&lt;/h2&gt;

&lt;p&gt;Console.log() is one method of debugging we can use. This is part of the browser's developer console panel. To access the developer console, you can &lt;em&gt;right click&lt;/em&gt; on the browser page, click &lt;em&gt;inspect&lt;/em&gt;, and go to the &lt;em&gt;console&lt;/em&gt; panel. (You can also click on the &lt;em&gt;view&lt;/em&gt; tab at the top of your computer, click &lt;em&gt;developer&lt;/em&gt;, and then &lt;em&gt;developer tools&lt;/em&gt;). Console.log() can be written in the browser's developer tools itself, or wherever your line of code is written. To use it in the browser's developer tools, follow along below:&lt;br&gt;
Say we had a variable set as an array:&lt;br&gt;
&lt;code&gt;const flowers = ['Orchids', 'Tulips', 'Roses'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can check that we declared it correctly by console logging &lt;code&gt;console.log(flowers);&lt;/code&gt; in our console. We should get an output of &lt;code&gt;(3) ['Orchids', 'Tulips', 'Roses']&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%2Fqj2n31359aj2odymk726.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%2Fqj2n31359aj2odymk726.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's what we got! We can see that our variable 'flowers' is now assigned to that array, so we can use it in future codes without worry.&lt;/p&gt;

&lt;p&gt;For more complex functions, we can implement our console.log() within the function and verify it logs to our console. Such as, if we had the following variable and event listener:&lt;br&gt;
&lt;code&gt;const btn = document.querySelector("#button")&lt;br&gt;
btn.addEventListner("click", function() {&lt;br&gt;
console.log ("I was clicked");&lt;br&gt;
})&lt;/code&gt;&lt;br&gt;
Once we invoke that function, when we click the button we should see in our console:&lt;br&gt;
&lt;code&gt;I was clicked&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%2Fkrruywg7k0u1o6fpgi8n.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%2Fkrruywg7k0u1o6fpgi8n.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was important to us because it confirmed that we properly set a variable, grabbed it, and assigned an event listener to it. Now that we know the button works, we can continue on writing what we would actually like it to render to the DOM.&lt;/p&gt;

&lt;p&gt;Essentially, console.log() will do just that- log what we choose in the console! &lt;/p&gt;
&lt;h2&gt;
  
  
  Using Debugger Keyword
&lt;/h2&gt;

&lt;p&gt;The debugger keyword is...(wait for it)... "debugger;". Like console logging, debugger can be done in the browser's developer tools(under the &lt;em&gt;source&lt;/em&gt; panel), or inputted into your line of code. The JavaScript code will run until it hits the debugger keyword, which acts like a breakpoint. Debugger allows us to pause the code, which means we also can resume the execution with the provided buttons (check out  &lt;a href="https://javascript.info/debugging-chrome" rel="noopener noreferrer"&gt;this link&lt;/a&gt;for a more descriptive walk through of how to use the buttons in developer's tools sources. Follow along in the example below to see how to implement debugger in your line of code. &lt;br&gt;
This is a basic function in our index.js to make the concept a bit easier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;math&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;  
 &lt;span class="k"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;math&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;(A debugger within a debugger! The reason for putting console.log is to just to show you where the debugger keyword stops the code, as shown later in this section)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we open our index.html in our browser, and navigate to our developer tool's source panel, when we run this code we should see 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%2Frowdu9vl689pjffzojb8.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%2Frowdu9vl689pjffzojb8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code stopped executing where we placed the debugger, so that we could inspect everything was running properly above it. We see the variables are returning the proper data so we can resume our debugger. (It's important to note that because it has stopped at the debugger and only able to read what was declared before it. Console.log(math()) value is not appearing in our console because it comes after the debugger)&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%2Fbye9xefrde9p0o9coab0.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%2Fbye9xefrde9p0o9coab0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we resume our execution of code, it will then give us the desired output.&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%2Fxqsfbh8vugccerkl9dvx.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%2Fxqsfbh8vugccerkl9dvx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To implement debugger in our developer's tools, we can choose where we would like to place it by using the pane in the right-hand side (or bottom depending on how wide your browser is) of the source panel. The YouTube Channel &lt;a href="https://www.youtube.com/watch?v=H0XScE08hy8" rel="noopener noreferrer"&gt;Google Chrome Developers&lt;/a&gt; does a great job of showing you where you can place the debugger.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use Console.log vs Debugger Keyword
&lt;/h2&gt;

&lt;p&gt;So which one is better? Well, that depends on what you already know about your code. It's nice using console.log() when you have a better understanding of your code and exactly where to place it. If you want to make sure your "click" event listener is working, console.log a message and then click on the button! I personally think console.log() is great for when you are checking as you go, practicing frequently.&lt;/p&gt;

&lt;p&gt;The debugger keyword is good to use when you get an error message and aren't really sure where the error could be. Instead of going back and console logging multiple things, it would be best to put in the debugger keyword to scan through a general area, and pause to take a look at each line of code. The debugger keyword as a debugging tool can be faster and more efficient if used in this way, and allows us to work with the code more than console logging does. &lt;/p&gt;

&lt;h2&gt;
  
  
  Now Go Debug Like an Exterminator
&lt;/h2&gt;

&lt;p&gt;In short, we went over that debugging is a tool we can use to help us find our errors and fix them. As a developer, a lot of our time is spent debugging and we need ways to make this process more efficient. We learned we can use our console.log() or debugging keyword to help us out, and the different situations we may use one over the other. It's important we debug as we go, in order to help ourselves out in the long run. Bugs will happen (and that's okay!), just remember to take it slow, work through the errors, and you'll be back to creating in no time!  &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
