<?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: Mandy Petrakis</title>
    <description>The latest articles on Forem by Mandy Petrakis (@mandy_petrakis).</description>
    <link>https://forem.com/mandy_petrakis</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%2F1001983%2Ffa2fa5fa-4164-4e0d-ab32-a1c3732a1ed9.png</url>
      <title>Forem: Mandy Petrakis</title>
      <link>https://forem.com/mandy_petrakis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mandy_petrakis"/>
    <language>en</language>
    <item>
      <title>Understanding Tanstack Query Keys</title>
      <dc:creator>Mandy Petrakis</dc:creator>
      <pubDate>Sat, 09 Dec 2023 01:34:16 +0000</pubDate>
      <link>https://forem.com/mandy_petrakis/understanding-tanstack-query-keys-2jfe</link>
      <guid>https://forem.com/mandy_petrakis/understanding-tanstack-query-keys-2jfe</guid>
      <description>&lt;p&gt;In React Query, the Query Key is a unique identifier that represents a specific query. They not only help you identify the query, but they also play a critical role in caching data.  At a high level, the Query Key is an array and can simply contain a single string or be more complex and contain nested objects. This identifier is used to manage the cache, track the status of queries, and enable efficient updates to the UI when data changes. This blog will give an understanding of that functionality and best practices on how to do that effectively so you can get React Query up and running in your project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Caching
&lt;/h3&gt;

&lt;p&gt;To understand the role Query Keys play and how to use them you have to understand that the Query Cache is an object. In this object, the keys are your Query Keys that have been serialized and the value is the data returned from your query. Because of this, your keys will need to be unique to that specific query otherwise when React Query finds an entry for that key, it will use the value rather than use the query function to return the data. &lt;/p&gt;

&lt;h3&gt;
  
  
  Invalidating
&lt;/h3&gt;

&lt;p&gt;In React Query, when you invalidate a query the Query Function refetches and updates the data associated with a particular query. When data in the underlying source changes, you can use the &lt;code&gt;queryClient.invalidateQueries&lt;/code&gt; method to mark a specific query or set of queries as stale. This triggers a refetch the next time the invalidated query is accessed.&lt;/p&gt;

&lt;p&gt;For instance, if a user updates an item in a to-do list, the corresponding query fetching that list can be invalidated using this method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;queryClient.invalidateQueries(queryKey);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By invalidating queries strategically, you ensure that the UI reflects the most up-to-date data without the need for a manual refresh. React Query's caching system then handles the refetch efficiently, maintaining a seamless and performant user experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Structure
&lt;/h3&gt;

&lt;p&gt;Structure your keys as an array from most generic to most specific, with as much detail as you see fit. Be sure to include any variables your function depends on. Consider breaking apart the URL with the information in each slash being a new element in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/posts &amp;gt; ["posts"]
/posts/1 &amp;gt; ["posts", post.id]
/posts?authorId=1 &amp;gt; ["posts", {authorId: 1}]
/posts/2/comments &amp;gt; ["posts", post.id, "comments"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this structure, you can invalidate everything related to posts with &lt;code&gt;["posts"]&lt;/code&gt; and any specific post.id.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dynamic
&lt;/h3&gt;

&lt;p&gt;One of the strengths of React Query is its ability to handle dynamic data fetching. Query keys can include dynamic parameters, allowing you to easily fetch data based on user input or other changing conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userId = getCurrentUserId(); // dynamic user ID const userQueryKey = ['user', userId];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the query key includes a dynamic &lt;code&gt;userId&lt;/code&gt; obtained at runtime, allowing you to fetch data for a specific user dynamically.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Setting up Bcrypt in your Rails project</title>
      <dc:creator>Mandy Petrakis</dc:creator>
      <pubDate>Wed, 23 Aug 2023 22:08:35 +0000</pubDate>
      <link>https://forem.com/mandy_petrakis/setting-up-bcrypt-in-your-rails-project-fn0</link>
      <guid>https://forem.com/mandy_petrakis/setting-up-bcrypt-in-your-rails-project-fn0</guid>
      <description>&lt;p&gt;If you're here you probably already know that storing passwords in plain text is a bad idea.  Here is a step-by-step guide on how to set up Bcrypt in your Rails project. It's quite simple, yet offers powerful security to your user's information as well as any sensitive data you might be storing in your database. It's so simple in fact, you'll probably have time to read about how it works at the end if you aren't already familiar. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; If you don't have your Rails project set up go ahead a run:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rails new ProjectName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add the 'bcrypt' Gem to your project's &lt;code&gt;Gemfile&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gem &lt;span class="s1"&gt;'bcrypt'&lt;/span&gt;, &lt;span class="s1"&gt;'~&amp;gt; 3.1.13'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your terminal, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bundle &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Generate the model you will be storing the password in and include &lt;code&gt;password_digest&lt;/code&gt; as an attribute. Remember the default type when using a generator is a string which is how the &lt;code&gt;password_digest&lt;/code&gt; will be stored.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rails g model User name email password_digest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the model, add your associations and validations and include &lt;code&gt;has_secure_password&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&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;User&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;has_secure_password&lt;/span&gt;

    &lt;span class="c1"&gt;# Include additional validations...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Migrate your database to create your &lt;code&gt;users&lt;/code&gt; table
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rails db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Implement user registration in your &lt;code&gt;users_controller&lt;/code&gt; with a create action along with your error handling. Though we included a &lt;code&gt;password_digest&lt;/code&gt; attribute in our &lt;code&gt;user&lt;/code&gt; table, we will still take in a &lt;code&gt;password&lt;/code&gt; and &lt;code&gt;password_confirmation&lt;/code&gt; in our &lt;code&gt;user_params&lt;/code&gt;. Bcrypt will do the work of hashing the plain text password and storing it as &lt;code&gt;password_digest&lt;/code&gt; thanks to that handy single line of code in step 4, &lt;code&gt;has_secure_password&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&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;UsersController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt; 

&lt;span class="n"&gt;rescue_from&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;RecordInvalid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;with: :render_record_invalid&lt;/span&gt;

     &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt; 
          &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
      &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;
        &lt;span class="c1"&gt;# Include any additional successful registration steps&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="kp"&gt;private&lt;/span&gt; 

     &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;user_params&lt;/span&gt; 
          &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password_confirmation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
     &lt;span class="k"&gt;end&lt;/span&gt; 

     &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;render_record_invalid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;errors: &lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;full_messages&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="ss"&gt;status: :unprocessable_entity&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;p&gt;So far the code above will take in new user information as &lt;code&gt;use_params&lt;/code&gt; and create a new user. It will verify that the &lt;code&gt;password&lt;/code&gt; and &lt;code&gt;password_digest&lt;/code&gt; match and save the new user to the database if so along with the Bcrypt generated &lt;code&gt;password_digest&lt;/code&gt;. If the password and confirmation do not match, the bang operator in &lt;code&gt;User.create!&lt;/code&gt; will raise an error that will be rescued with the render_record_invalid function to return any errors so we can render them to the dom for our user. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lastly, we will authenticate our users when they sign in. This will go into the cont
roller responsible for your user authentication, such as a &lt;code&gt;sessions_controller&lt;/code&gt;. First, the action will find the &lt;code&gt;user&lt;/code&gt; via their email, in this example and authenticate the password they input against the store &lt;code&gt;password_digest&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&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;SessionsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt; 
     &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt; 
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;email: &lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; 
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; 
        &lt;span class="c1"&gt;# Handle successful login &lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; 
        &lt;span class="c1"&gt;# Handle login failure &lt;/span&gt;
     &lt;span class="k"&gt;end&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;p&gt;Thats it! Your passwords are being stored securely.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works:
&lt;/h2&gt;

&lt;p&gt;Bcrypt is a hashing algorithm that takes a bit of data (e.g. password) and creates a "digital fingerprint" from it. In other words 'password' becomes something like &lt;code&gt;2b$10$nOUIs5kJ7naTuTFkBy1veuK0kSxUFXfuaOKdOKf9xYT0KKIGSJwFa&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What makes Bcrypt more secure is that it adds "salt" to the password before hashing it. Salt is a random bit of additional data that is prepended to the password the user entered before it is hashed. This looks like turning our password into &lt;code&gt;E4OAovh7rbpassword&lt;/code&gt; before processing it through the hashing algorithm. This way, if two people have have the same password, the salt ensures that their &lt;code&gt;password_digest&lt;/code&gt; is completely different.&lt;/p&gt;

&lt;p&gt;So how do we authenticate the user?&lt;/p&gt;

&lt;p&gt;Bcrypt stores the password salt prepended on the password hash in the &lt;code&gt;password_digest&lt;/code&gt; so all we have to do is add that salt to the password the user entered upon log in and see if it matches. Yes, the stored hash does have embedded information about its computation. As seen in the example below from the Bcrypt official npm page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$2b$10$nOUIs5kJ7naTuTFkBy1veuK0kSxUFXfuaOKdOKf9xYT0KKIGSJwFa&lt;/span&gt;
 |  |  |                     |
 |  |  |                     hash-value &lt;span class="o"&gt;=&lt;/span&gt; K0kSxUFXfuaOKdOKf9xYT0KKIGSJwFa
 |  |  |
 |  |  salt &lt;span class="o"&gt;=&lt;/span&gt; nOUIs5kJ7naTuTFkBy1veu
 |  |
 |  cost-factor &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 10 &lt;span class="o"&gt;=&lt;/span&gt; 2^10 rounds
 |
 hash-algorithm identifier &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 2b &lt;span class="o"&gt;=&lt;/span&gt; BCrypt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read more about Bcrypt for Ruby &lt;a href="//a://github.com/bcrypt-ruby/bcrypt-ruby"&gt;here.&lt;/a&gt; &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>ruby</category>
      <category>rails</category>
      <category>security</category>
    </item>
    <item>
      <title>Handling associated records when an object is deleted</title>
      <dc:creator>Mandy Petrakis</dc:creator>
      <pubDate>Tue, 23 May 2023 19:00:57 +0000</pubDate>
      <link>https://forem.com/mandy_petrakis/handling-associated-records-when-an-object-is-deleted-1end</link>
      <guid>https://forem.com/mandy_petrakis/handling-associated-records-when-an-object-is-deleted-1end</guid>
      <description>&lt;p&gt;In phase 3 of my coding boot camp, we scratched the surface of database management with the help of Active Record. I have to say, Active Record is the real MVP and maybe I'm not looking in the right places but I don't see it getting a ton of love online so shout out to Active Record. Until it came into the picture I found myself thinking things like "Maybe the backend isn't for me", "There's no way people write this much SQL" and "There has to be a better way". Fortunately, that last thought was correct. Active Record provides us with a powerful set of tools to simplify many processes, including how to handle children of objects you delete from your database. This came in handy when working on a project and in this blog we will explore ways to handle all records associated with an object using some built-in features of Active Record. &lt;/p&gt;

&lt;p&gt;First things first - for there to be a parent/child relationship in your database, your tables have to be associated. Associations define relationships between different models in a database. The most common association types include &lt;code&gt;has_many&lt;/code&gt;, &lt;code&gt;belongs_to&lt;/code&gt;, &lt;code&gt;has_one&lt;/code&gt;, and &lt;code&gt;has_and_belongs_to_many&lt;/code&gt;. These associations establish connections between tables, allowing us to navigate relationships and fetch associated data with ease. Your association is declared in your model classes like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Owner &amp;lt; ActiveRecord::Base

has_many :items

end
&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;class Item &amp;lt; ActiveRecord::Base

belongs_to :owner

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

&lt;/div&gt;



&lt;p&gt;This example is a one-to-many association where an Owner has many Items and each item belongs to one owner. Associations give us access to methods that simplify database interactions such as effortlessly querying, building, creating, updating, and deleting associated objects. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;:dependent&lt;/code&gt; option in Active Record associations allows you to specify the behavior of associated records when the parent object is deleted. The cool thing is,  it provides flexibility and control over the deletion process with a few different options. Here is an overview of what they are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;code&gt;:destroy&lt;/code&gt;: Calls the &lt;code&gt;destroy&lt;/code&gt; method on each associated record, triggering associated callbacks and validations. This option allows for cascading deletion of associated records. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(Side note: Cascading deletion simply refers to the automatic deletion of associated records when the parent object is deleted. This deletion "cascades" down to the associated records, ensuring data integrity and consistency. It simplifies the process of deleting complex data structures and prevents orphaned records.)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;:delete_all&lt;/code&gt;: For scenarios where performance is critical, the &lt;code&gt;:delete_all&lt;/code&gt; option comes in handy. It performs a single SQL DELETE statement to directly remove all associated records without triggering callbacks or validations. This approach offers a significant performance boost when deleting a large number of associated records.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;:nullify&lt;/code&gt;: In certain cases, rather than deleting associated records, we may prefer to nullify the foreign key column of those records. The &lt;code&gt;:nullify&lt;/code&gt; option achieves this by updating the foreign key column of associated records to &lt;code&gt;NULL&lt;/code&gt; without deleting them. This option preserves the data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;:restrict_with_exception&lt;/code&gt; and  &lt;code&gt;:restrict_with_error&lt;/code&gt;: To enforce constraints on deletion, Active Record provides &lt;code&gt;:restrict_with_exception&lt;/code&gt; and &lt;code&gt;:restrict_with_error&lt;/code&gt;. When the &lt;code&gt;:restrict_with_exception&lt;/code&gt; option is used, an exception is raised if associated records exist, preventing the deletion of the parent object until the associated records are removed. On the other hand, &lt;code&gt;:restrict_with_error&lt;/code&gt; adds an error to the parent object, preventing its deletion.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Owner &amp;lt; ActiveRecord::Base

has_many :items, dependent: :destroy

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

&lt;/div&gt;



&lt;p&gt;In the above example, when an owner is deleted, all associated items will also be destroyed due to the &lt;code&gt;:dependent&lt;/code&gt; option set to &lt;code&gt;:destroy&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;These options offer flexibility in managing associated records during deletion, allowing you to choose the approach that best suits your needs. Remember to consider the impact on your application's performance when if you may need that data in the future when deciding on a &lt;code&gt;:dependent&lt;/code&gt; option!&lt;/p&gt;

</description>
      <category>activerecord</category>
      <category>beginners</category>
      <category>backend</category>
    </item>
    <item>
      <title>Context, Simply Put</title>
      <dc:creator>Mandy Petrakis</dc:creator>
      <pubDate>Mon, 20 Feb 2023 20:36:55 +0000</pubDate>
      <link>https://forem.com/mandy_petrakis/context-simply-put-16go</link>
      <guid>https://forem.com/mandy_petrakis/context-simply-put-16go</guid>
      <description>&lt;p&gt;When I started to learn React, I was overwhelmed by the concept of Context so I immediately forgot everything I read and decided I would just never use it. Then I had to drill state and setState 5+ components deep in a bootcamp project and I thought maybe I should give Context another chance. At that point I was more familiar with state and component trees and armed with that confidence (and a playlist of youTube videos) I set out to understand and implement Context in my app, and lets just say I nailed it. Here is my simplified explanation for anyone just starting out with Context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Context?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Context is a way of accessing data through your whole component tree without having to pass props down manually at every level. Once Context is created you can import it in any component you wish to access the data. Simply put, its like creating a global state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to use Context?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I had read a few suggestions to use Context sparingly which was part of the reason I didn't spend time learning it initially, and frankly, I don't heed that advice now. For me, Context makes my code feel a lot cleaner and better managed.  If I need access to a piece of state in 3 or more components, I'm using Context. That is especially true if I'm going update the state and I want the change to persist across all pages of my app. Common examples people use for Context are setting a dark theme and referencing if a  user logged in. I personally use it often with data I have fetched from an API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to do it:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will need to do two things to use Context: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Establish Context with &lt;code&gt;createContext&lt;/code&gt; and wrapping components that will use the context in a &lt;code&gt;Context.Provider&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Importing the context in the consumer component with &lt;code&gt;useContext&lt;/code&gt;. (With a bonus method of importing Context at the end!)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First, decide where you want to store your context data. I like to put it in a separate Context component and import it to my App component. This keeps my code tidy but you can also establish Context directly in your App component. Like state, you just want to make sure it lives in a parent to all the components that will eventually import the data. If you are storing context in a Context component like me, you will need to pass it a &lt;code&gt;{children}&lt;/code&gt; prop. &lt;/p&gt;

&lt;p&gt;In the Context component import &lt;code&gt;createContext&lt;/code&gt; from react with your other hooks. In this example, I'm going to &lt;code&gt;useState&lt;/code&gt; to store the data we will pass as context to the consumer components. &lt;/p&gt;

&lt;p&gt;Next we will need to use the &lt;code&gt;createContext&lt;/code&gt; hook to declare our context &lt;em&gt;outside&lt;/em&gt; our Context component using the &lt;code&gt;createContext()&lt;/code&gt; function. We declare the context outside the Context component because we are going to export it for use in our consumer components. &lt;/p&gt;

&lt;p&gt;Your code should look like this so far:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbf2cntgk1156w4wh40bj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbf2cntgk1156w4wh40bj.png" alt="One" width="800" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to tell React what data our &lt;code&gt;DataContext&lt;/code&gt; we just created will hold. This is usually where I would &lt;code&gt;useEffects&lt;/code&gt; to fetch data from an API and assign it to a state variable but to keep our example clean and simple I'm just going to declare the state with a default value of an array of numbers. &lt;/p&gt;

&lt;p&gt;From here we create the 'provider component' for the &lt;code&gt;DataContext&lt;/code&gt; as a return from our Context component. This &lt;code&gt;DataContext.Provider&lt;/code&gt; component takes one prop called &lt;code&gt;value&lt;/code&gt; which we assign &lt;code&gt;data&lt;/code&gt; (our state from the previous step) and wraps around the &lt;code&gt;{children}&lt;/code&gt; prop we gave to our Context component earlier. Like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fqah5ujvfnw72wpbszixx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqah5ujvfnw72wpbszixx.png" alt="Two" width="800" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What if we wanted to use &lt;code&gt;setData&lt;/code&gt; in another component of the app? Well, we could declare another context and call it something like &lt;code&gt;UpdateDataContext&lt;/code&gt; and wrap both our child and &lt;code&gt;DataContext.Provider&lt;/code&gt; in the &lt;code&gt;UpdateDataContext.Provider&lt;/code&gt;. If this were unrelated data, I would probably go ahead and do that.&lt;/p&gt;

&lt;p&gt;Instead since &lt;code&gt;setData&lt;/code&gt; is part of our data state, in our &lt;code&gt;DataContext.Provider&lt;/code&gt; we set our value to an array of &lt;code&gt;[data, setData]&lt;/code&gt;. This will pass both our &lt;code&gt;data&lt;/code&gt; and &lt;code&gt;setData&lt;/code&gt; function to our children components in our single &lt;code&gt;DataContext.Provider&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fh0c05amidx6qqonwfscl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fh0c05amidx6qqonwfscl.png" alt="Three" width="800" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final step in our provider component process is to import our Context component to our App component and wrap it around all the children, or 'consumer components' we want to pass our data to. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fghe781itp06hs6a96zlt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fghe781itp06hs6a96zlt.png" alt="Four" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this stage, our &lt;code&gt;data&lt;/code&gt; and &lt;code&gt;setData&lt;/code&gt; are available to all the components in our app without having to pass any props! Amazing, but how do we access them? Thats step 2...&lt;/p&gt;

&lt;p&gt;useContext&lt;/p&gt;

&lt;p&gt;Lets go directly to ComponentC, bypassing components A and B. Here we will import &lt;code&gt;useContext&lt;/code&gt; from react as well as our &lt;code&gt;DataContext&lt;/code&gt; from our Context component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvkfc5ixui2gc3m675tvp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvkfc5ixui2gc3m675tvp.png" alt="Five" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll then take the &lt;code&gt;useContext&lt;/code&gt; hook, pass it &lt;code&gt;DataContext&lt;/code&gt; and declare the variables we will be setting the data to in ComponentC. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvo7vgehi0emi5nczgeja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvo7vgehi0emi5nczgeja.png" alt="Six" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Viola! You now have access to your &lt;code&gt;data&lt;/code&gt; state and &lt;code&gt;setData&lt;/code&gt; function in  ComponenetC to use as you wish with out having to pass the prop through multiple components. &lt;/p&gt;

&lt;p&gt;TLDR: &lt;/p&gt;

&lt;p&gt;To recap, in order to create globally accessible state with Context in your app we:&lt;/p&gt;

&lt;p&gt;Created the Context Provider by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating a Context component and passing it a prop of &lt;code&gt;{children}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Importing &lt;code&gt;createContext&lt;/code&gt; from react&lt;/li&gt;
&lt;li&gt;Declaring and exporting our &lt;code&gt;DataContext&lt;/code&gt; from &lt;code&gt;createContext()&lt;/code&gt; outside the Context component. &lt;/li&gt;
&lt;li&gt;Declaring the state we will use as our context data inside the Context component.&lt;/li&gt;
&lt;li&gt;Wrapping our &lt;code&gt;{children}&lt;/code&gt; in the &lt;code&gt;DataContext.Provider&lt;/code&gt; that has one prop of value and setting it to the state we declared in step 4. &lt;/li&gt;
&lt;li&gt;Exporting our Context component and importing it in the App component to wrap around all the components in our app. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Accessed the Context in our consumer component by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Importing &lt;code&gt;useContext&lt;/code&gt; from react and &lt;code&gt;DataContext&lt;/code&gt;
from our Context component in our consumer component. &lt;/li&gt;
&lt;li&gt;Assigning our &lt;code&gt;data&lt;/code&gt; and &lt;code&gt;setData&lt;/code&gt; variables using &lt;code&gt;useContext&lt;/code&gt; and passing it our &lt;code&gt;DataContext&lt;/code&gt;. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Bonus!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The above works just fine but one more thing I like to do to streamline my code is rather than importing &lt;code&gt;useContext&lt;/code&gt; in my consumer, I export a function that returns &lt;code&gt;useContext()&lt;/code&gt; from my Context component so I only have to import this function to to access my data in my consumer components. This is what it looks like in both &lt;code&gt;Context&lt;/code&gt; and &lt;code&gt;ComponenetC&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fh9nh2v7tez5r65ztrdxn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fh9nh2v7tez5r65ztrdxn.png" alt="Seven" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we did was:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import &lt;code&gt;useContext&lt;/code&gt; in the &lt;code&gt;Context&lt;/code&gt; component and remove it from &lt;code&gt;ComponentC&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Export the &lt;code&gt;useData&lt;/code&gt; function that returns &lt;code&gt;useContext(DataContext)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Imported &lt;code&gt;useData&lt;/code&gt; in ComponentC.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thats it! After setting up Context once or twice I felt a lot more confident but I did reference my notes on future tries so write your own workflow or save this blog for future reference!&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>When not to use forEach()</title>
      <dc:creator>Mandy Petrakis</dc:creator>
      <pubDate>Thu, 05 Jan 2023 22:41:41 +0000</pubDate>
      <link>https://forem.com/mandy_petrakis/when-not-to-use-foreach-5aj2</link>
      <guid>https://forem.com/mandy_petrakis/when-not-to-use-foreach-5aj2</guid>
      <description>&lt;p&gt;The first phase of my coding boot camp was a doozy. I learned early and fast that documentation is my best friend as a newbie developer (and I do suspect we will grow old together). I looked at my google search history and I found I was a frequent flier on all the array iterator MDN pages. They were SO helpful, but more than once I found myself building functions to do exactly what they do. On top of their functionality, I had to get better at recognizing when to use which one and more specifically, when not to use &lt;code&gt;forEach()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  forEach()
&lt;/h2&gt;

&lt;p&gt;Like I implied, &lt;code&gt;forEach()&lt;/code&gt; was my catch-all. It was the enabler to my "I want to take the ugly long way, and maybe not even get where I'm going" habit. Don't get me wrong, once I stopped abusing our friendship &lt;code&gt;forEach()&lt;/code&gt; and I got on great. It is particularly useful when you want to perform an action on each element of an array, but you don't need to return a new array or you want to update the elements of the original array.&lt;/p&gt;

&lt;p&gt;Here is an example: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fw0k1t0cyfn8jkdghrr9l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw0k1t0cyfn8jkdghrr9l.png" alt="forEach" width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see the original array was destructively updated.&lt;/p&gt;

&lt;p&gt;Something to note is that the return value of &lt;code&gt;forEach()&lt;/code&gt; is undefined. Therefore, in the example above, had we saved it to a newArray variable, the value of that new variable would be undefined.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffxqek5gbwiyiiw4auz1v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffxqek5gbwiyiiw4auz1v.png" alt="forEach undefined" width="800" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you find yourself returning a value in your &lt;code&gt;forEach()&lt;/code&gt;, it can be a helpful clue to another method you can use to do some work for you and make your code a little cleaner and more expressive.&lt;/p&gt;

&lt;h2&gt;
  
  
  .map()
&lt;/h2&gt;

&lt;p&gt;If you are returning an array in your &lt;code&gt;forEach()&lt;/code&gt;, you may be better off using &lt;code&gt;map()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;map()&lt;/code&gt; array iterator applies a function to each element of an array and returns a new array with the results. The original array is not modified.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fh34qektvy2olzmy89u5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fh34qektvy2olzmy89u5p.png" alt=".map" width="800" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A critical step I missed in the beginning (as in, two weeks ago- I've grown so much) was the necessity of saving the &lt;code&gt;.map()&lt;/code&gt; to a variable to use later. Like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1j9ov22wm6v02zjv5nn7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1j9ov22wm6v02zjv5nn7.png" alt=".map variable" width="800" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Applying &lt;code&gt;map()&lt;/code&gt; returns the new array and assigning it to a variable lets you use it later on. If you didn't save it to a variable, did you even &lt;code&gt;map()&lt;/code&gt;?&lt;/p&gt;

&lt;h2&gt;
  
  
  .find()
&lt;/h2&gt;

&lt;p&gt;Next, if your &lt;code&gt;forEach()&lt;/code&gt; has an if statement that returns the item try using &lt;code&gt;find()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;find()&lt;/code&gt; returns the value of the first element in the array that passes the test implemented by the provided function. If no element passes the test, it returns &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7bo4qvnzuhmeogm5mh6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7bo4qvnzuhmeogm5mh6j.png" alt=".find" width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also use &lt;code&gt;find()&lt;/code&gt; when you want to find an element in an array based on its properties. For example, the following code uses &lt;code&gt;find()&lt;/code&gt; to find the first user in an array of users with a certain name:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdztegfgldx7vydw63wxl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdztegfgldx7vydw63wxl.png" alt="find 2" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In general, you should use the &lt;code&gt;find()&lt;/code&gt; array iterator when you want to find the first element in an array that satisfies a certain condition, and you only need to return the value of that element.&lt;/p&gt;

&lt;h2&gt;
  
  
  .filter()
&lt;/h2&gt;

&lt;p&gt;If your &lt;code&gt;forEach()&lt;/code&gt; contains an if statement that pushes the element to a new array, you've done a lot of work but you could have used &lt;code&gt;filter()&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;filter()&lt;/code&gt; creates a new array with all the elements that pass the test implemented by the provided function. If no element passes the test, it returns an empty array.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;code&gt;filter()&lt;/code&gt; array iterator when you want to filter an array based on the properties of its elements. For example, the following code uses &lt;code&gt;filter()&lt;/code&gt; to get all the users from an array of users who are over 30 years old:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fnqbwn0a139bylkbr6y7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnqbwn0a139bylkbr6y7b.png" alt="filter" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  .reduce()
&lt;/h2&gt;

&lt;p&gt;The last method we'll look at (there are more), is &lt;code&gt;reduce()&lt;/code&gt;. If your &lt;code&gt;forEach&lt;/code&gt; is returning the sum or product of an array, this method is for you.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;reduce()&lt;/code&gt; array iterator executes a reducer function (that you provide) on each element of the array, resulting in a single output value. The reducer function takes four arguments. The first argument is the accumulator, which "accumulates" all the return values after the reducer function is invoked. The second argument is the current value you are working with and the last two arguments are optional (the current index and the array).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fw9b4auds5wk6ohlvnxam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw9b4auds5wk6ohlvnxam.png" alt=".reduce" width="800" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach" rel="noopener noreferrer"&gt;forEach()&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" rel="noopener noreferrer"&gt;.map()&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find" rel="noopener noreferrer"&gt;.find()&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" rel="noopener noreferrer"&gt;.filter()&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce" rel="noopener noreferrer"&gt;.reduce()&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
