<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Min</title>
    <description>The latest articles on DEV Community by Min (@minchulan).</description>
    <link>https://dev.to/minchulan</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%2F905594%2Fcc617b73-603f-4309-bc57-a6559bb61346.jpeg</url>
      <title>DEV Community: Min</title>
      <link>https://dev.to/minchulan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/minchulan"/>
    <language>en</language>
    <item>
      <title>Building a Full Stack Application with Ruby on Rails and React within the MVC Architecture</title>
      <dc:creator>Min</dc:creator>
      <pubDate>Fri, 20 Oct 2023 17:44:04 +0000</pubDate>
      <link>https://dev.to/minchulan/building-a-full-stack-mvc-application-with-ruby-on-rails-and-react-4n9n</link>
      <guid>https://dev.to/minchulan/building-a-full-stack-mvc-application-with-ruby-on-rails-and-react-4n9n</guid>
      <description>&lt;p&gt;In the ever-evolving landscape of web development, constructing user-friendly platforms is crucial. One key to achieving this lies in understanding and implementing architectural patterns like the Model-View-Controller (MVC) architecture. MVC divides an application into three interconnected components: the Model, the View, and the Controller.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding the Model-View-Controller (MVC) Architecture:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Model&lt;/strong&gt;: The Model represents the application's data and logic. In our case, it's the backend logic and the database in Ruby on Rails. In the example below, the &lt;code&gt;Post&lt;/code&gt; model encapsulates the data structure, defining attributes like &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;content&lt;/code&gt;. It ensures data integrity, validating user inputs and managing interactions with the database.&lt;br&gt;
&lt;strong&gt;View&lt;/strong&gt;: The View focuses on presenting the data to users. In our setup, React components, such as the &lt;code&gt;PostsList&lt;/code&gt;, act as the View. They render the data fetched from the backend, providing the visual interface users interact with.&lt;br&gt;
&lt;strong&gt;Controller&lt;/strong&gt;: The Controller serves as an intermediary between the Model and the View. In our architecture, the Rails &lt;code&gt;PostsController&lt;/code&gt; takes center stage. It receives requests from the frontend, interacts with the Model to retrieve or store data, and then sends the processed data to the View for display. The Controller ensures seamless communication between the backend and the frontend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, Let's Dive Deeper:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set up the Backend with Ruby on Rails:&lt;/strong&gt; &lt;br&gt;
Ruby on Rails is renowned for its convention-over-configuration approach. Here's how we initiate our backend development:&lt;/p&gt;

&lt;p&gt;Step 1: Generate a new Rails application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails new myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Create a Post model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails g model Post title:string content:text
$ rails db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: Define the API endpoints in the controller:&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="c1"&gt;# app/controllers/posts_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PostsController&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;index&lt;/span&gt;
    &lt;span class="vi"&gt;@posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="vi"&gt;@posts&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;create&lt;/span&gt;
    &lt;span class="vi"&gt;@post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
      &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="vi"&gt;@post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;status: :created&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="vi"&gt;@post&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="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;span class="kp"&gt;private&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;post_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;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:post&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;:title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:content&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;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we define the &lt;code&gt;PostsController&lt;/code&gt; with the &lt;code&gt;index&lt;/code&gt; and &lt;code&gt;create&lt;/code&gt; actions. The &lt;code&gt;index&lt;/code&gt; action retrieves all posts from the database and renders them as a JSON response. The &lt;code&gt;create&lt;/code&gt; action creates a new post based on the parameters received and returns the created post or error messages in JSON format.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configuring the Frontend with React:&lt;/strong&gt;&lt;br&gt;
React, a popular JavaScript library for building user interfaces, integrates seamlessly with our Rails backend. Here's how you set it up:&lt;/p&gt;

&lt;p&gt;Step 1: Set up a React application using Create React App&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="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="nx"&gt;myapp_frontend&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Create a &lt;code&gt;PostsList&lt;/code&gt; component to fetch and display posts:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;PostsList&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;posts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPosts&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&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;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;posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&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;div&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;post&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="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&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;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&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;/h2&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;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&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;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;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;PostsList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;PostsList&lt;/code&gt; component, we use React's &lt;code&gt;useEffect&lt;/code&gt; hook to fetch the posts data from the Rails backend API endpoint (&lt;code&gt;/posts&lt;/code&gt;) when the component mounts. The fetched data is stored in the &lt;code&gt;posts&lt;/code&gt; state variable and then mapped to render each post's title and content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integrating Backend and Frontend in the MVC Architecture:&lt;/strong&gt;&lt;br&gt;
To unify our Rails backend and React frontend, we configure the frontend to communicate with the backend API endpoints via the routes.rb file. Linking React components to these endpoints enables seamless data exchange between the frontend and backend.&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="c1"&gt;# config/routes.rb&lt;/span&gt;
&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:posts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:create&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;This sets up the necessary routes for the &lt;code&gt;PostsController&lt;/code&gt; actions.&lt;/p&gt;

&lt;p&gt;Lastly, incorporate the &lt;code&gt;PostsList&lt;/code&gt; component into your React application to display the posts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
By harnessing the capabilities of Ruby on Rails as the backend framework and React as the frontend library within the MVC architecture, we establish a solid foundation for a full stack web application. This overview only covers the essentials of this integration. Rails manages the backend intricacies, ensuring data accuracy and consistency, while React empowers us with a dynamic and interactive user interface. Although this overview provides just a glimpse, the potential of this integration in web development is vast and exciting. &lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Query Like a Pro: Active Record for Database Queries</title>
      <dc:creator>Min</dc:creator>
      <pubDate>Sun, 24 Sep 2023 00:46:06 +0000</pubDate>
      <link>https://dev.to/minchulan/exploring-rails-active-record-and-querying-with-relational-databases-3p2g</link>
      <guid>https://dev.to/minchulan/exploring-rails-active-record-and-querying-with-relational-databases-3p2g</guid>
      <description>&lt;p&gt;Ruby on Rails is a popular web application framework known for its convention over configuration approach. One of the key components of Rails is Active Record, which provides an intuitive and powerful way to interact with relational databases. In this blog, we will explore the fundamentals of Rails Active Record and delve into querying data using a relational database. Code examples are alsp provided to help better understand  code examples to help you understand and utilize Active Record effectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Active Record:&lt;/strong&gt;&lt;br&gt;
Active Record is the Object-Relational Mapping (ORM) layer in Ruby on Rails. It facilitates the interaction between Ruby objects and the underlying relational database. Active Record automatically maps database tables to Ruby classes and provides methods for data manipulation, querying, and establishing relationships between models.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connecting to a Database:&lt;/strong&gt;&lt;br&gt;
To establish a connection with a relational database, Rails uses the &lt;code&gt;config/database.yml&lt;/code&gt; file, which contains database configuration details. By default, Rails supports popular databases like PostgreSQL, MySQL, and SQLite.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Defining Models and Migrations:&lt;/strong&gt;&lt;br&gt;
Models in Rails represent database tables. They encapsulate business logic and provide an interface for querying and manipulating data. Migrations are used to define and modify the database schema. Here's an example of creating a &lt;code&gt;User&lt;/code&gt; model and its corresponding migration:&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="c1"&gt;# Create User model&lt;/span&gt;
&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;generate&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="ss"&gt;:string&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="ss"&gt;:string&lt;/span&gt;

&lt;span class="c1"&gt;# Run migration&lt;/span&gt;
&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="ss"&gt;:migrate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Querying Data with Active Record:&lt;/strong&gt;&lt;br&gt;
Active Record provides a concise and readable syntax for querying data from the database. It offers a wide range of querying methods to fetch records that match specific conditions. Let's look at some common querying scenarios using Active Record:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retrieving All Records:&lt;/strong&gt;&lt;br&gt;
To fetch all records from the &lt;code&gt;users&lt;/code&gt; table, you can use the following code:&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;users&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;all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Retrieving Specific Records:&lt;/strong&gt;&lt;br&gt;
To retrieve records that meet specific conditions, you can use the &lt;code&gt;where&lt;/code&gt; method. For example, to get all users with the name "John":&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;johns&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;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Chaining Conditions:&lt;/strong&gt;&lt;br&gt;
Active Record allows you to chain multiple conditions together. For example, to find users named "John" with an email ending in "@example.com":&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;johns&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;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"email LIKE ?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"%@example.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Ordering Results:&lt;/strong&gt;&lt;br&gt;
You can order the query results using the &lt;code&gt;order&lt;/code&gt; method. To retrieve users in ascending order of their names:&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;users&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;order&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Limiting and Offsetting:&lt;/strong&gt;&lt;br&gt;
To limit the number of records returned or skip a certain number of records, you can use the &lt;code&gt;limit&lt;/code&gt; and &lt;code&gt;offset&lt;/code&gt; methods. For example, to fetch the first five users:&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;users&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;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Aggregating Data:&lt;/strong&gt;&lt;br&gt;
Active Record provides methods for aggregating data, such as counting records, calculating averages, and summing values. For instance, to count the number of users:&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;count&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;count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Finding a Single Record:&lt;/strong&gt;&lt;br&gt;
To find a single record based on a specific condition, you can use the find_by method. For example, to find a user with the email "&lt;a href="mailto:john@example.com"&gt;john@example.com&lt;/a&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="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="s2"&gt;"john@example.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This will return the first user that matches the condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Selecting Specific Columns:&lt;/strong&gt;&lt;br&gt;
You can specify which columns to retrieve from the database using the select method. This is useful when you only need specific attributes of the records. For example, to get the names of all users:&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;names&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;select&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This will return an array of user objects with only the name attribute loaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Using OR Conditions:&lt;/strong&gt;&lt;br&gt;
Active Record allows you to construct complex queries with OR conditions using the or method. For example, to find users named "John" or "Jane":&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;johns_and_janes&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;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;or&lt;/span&gt;&lt;span class="p"&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;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This will retrieve users with the name "John" or "Jane."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Count with Conditions:&lt;/strong&gt;&lt;br&gt;
You can count records that meet specific conditions using the where method in combination with count. For example, to count the number of users with the name "John":&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;count&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;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This will return the count of users with the name "John."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finding Records Within a Range:&lt;br&gt;
You can find records within a numeric or date range using the where method. For instance, to find all users created between two dates:&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;start_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"2023-01-01"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;end_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"2023-12-31"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;users_in_range&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;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;created_at: &lt;/span&gt;&lt;span class="n"&gt;start_date&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;end_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This retrieves users whose creation dates fall within the specified range.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Grouping and Counting:&lt;/strong&gt;&lt;br&gt;
Active Record enables you to group records by a specific attribute and count the occurrences. For example, to count the number of users per email domain:&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;email_counts&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;group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:email_domain&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This will return a hash with email domains as keys and the corresponding count of users.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;As you can see, Active Record simplifies database interactions in Ruby on Rails by providing a convenient and intuitive way to query and manipulate data. The examples above showcase the versatility of Active Record in querying data from relational databases. This blog only scratches the surface of what Active Record can do, but it should provide you with a foundation to start working with relational databases in Rails.&lt;/p&gt;

&lt;p&gt;Happy coding! &lt;/p&gt;

</description>
      <category>rails</category>
      <category>activerecord</category>
      <category>database</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Object-Oriented Ruby Programming: A Beginner's Guide</title>
      <dc:creator>Min</dc:creator>
      <pubDate>Sun, 24 Sep 2023 00:30:11 +0000</pubDate>
      <link>https://dev.to/minchulan/object-oriented-programming-with-ruby-a-beginners-guide-cp6</link>
      <guid>https://dev.to/minchulan/object-oriented-programming-with-ruby-a-beginners-guide-cp6</guid>
      <description>&lt;p&gt;Object-Oriented Programming (OOP) is a powerful paradigm that allows developers to organize code into reusable and modular structures. Ruby, with its elegant syntax and dynamic nature, is an excellent language for implementing OOP principles. In this blog, we will explore the foundations of Object-Oriented Ruby and how it enables us to build efficient and scalable applications. We will dive into classes, objects, instance variables and methods, class variables and methods, self, and demonstrate how these concepts come together to enhance code organization and reusability. Let's get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Classes and Objects:&lt;/strong&gt;&lt;br&gt;
In Ruby, everything is an object, and classes act as blueprints for creating objects. Let's take a closer look at classes and objects:&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&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;greet&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello, &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt; &lt;span class="c1"&gt;# Output: Hello, John!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we define a &lt;code&gt;Person&lt;/code&gt; class with an &lt;code&gt;initialize&lt;/code&gt; method that takes a parameter &lt;code&gt;name&lt;/code&gt; and assigns it to the instance variable &lt;code&gt;@name&lt;/code&gt;. The &lt;code&gt;initialize&lt;/code&gt; method is a special method in Ruby that gets called when a new object of the class is created using the &lt;code&gt;new&lt;/code&gt; method. We create a new object of the &lt;code&gt;Person&lt;/code&gt; class and call the &lt;code&gt;greet&lt;/code&gt; method on it. This demonstrates the fundamental concept of OOP, where objects are instances of classes and can invoke methods defined within those classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Instance Variables and Methods:&lt;/strong&gt;&lt;br&gt;
Instance variables, denoted with the &lt;code&gt;@&lt;/code&gt; symbol, are used to store data that is unique to each object instance. Instance methods are defined within the class and can access and manipulate instance variables. Let's see an example:&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&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;introduce&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello, my name is &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;."&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;introduce&lt;/span&gt; &lt;span class="c1"&gt;# Output: Hello, my name is Jane.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the &lt;code&gt;Person&lt;/code&gt; class has an instance variable &lt;code&gt;@name&lt;/code&gt;, which stores the name of the person. The &lt;code&gt;introduce&lt;/code&gt; method accesses the &lt;code&gt;@name&lt;/code&gt; instance variable and outputs a greeting along with the name of the person.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Class Variables and Methods:&lt;/strong&gt;&lt;br&gt;
In addition to instance variables and methods, Ruby also supports class variables and methods. Class variables, denoted with &lt;code&gt;@@&lt;/code&gt;, are shared among all instances of a class. Class methods are defined at the class level and can be called on the class itself rather than an instance of the class. Let's see an example:&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="vc"&gt;@@count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
    &lt;span class="vc"&gt;@@count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;total_count&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Total number of people: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vc"&gt;@@count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;person1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;person2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;total_count&lt;/span&gt; &lt;span class="c1"&gt;# Output: Total number of people: 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the &lt;code&gt;Person&lt;/code&gt; class has a class variable &lt;code&gt;@@count&lt;/code&gt;, which keeps track of the total number of instances created. The &lt;code&gt;initialize&lt;/code&gt; method increments the &lt;code&gt;@@count&lt;/code&gt; variable whenever a new object is created. The &lt;code&gt;total_count&lt;/code&gt; method is a class method denoted with &lt;code&gt;self&lt;/code&gt;, which can be called on the class itself to display the total count.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Self:&lt;/strong&gt;&lt;br&gt;
The keyword &lt;code&gt;self&lt;/code&gt; refers to the current object or class within Ruby code. It allows us to access the current object's instance variables and invoke its methods. Let's see an example:&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&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;introduce&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello, my name is &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&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;name&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;introduce&lt;/span&gt; &lt;span class="c1"&gt;# Output: Hello, my name is Alice.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the &lt;code&gt;introduce&lt;/code&gt; method uses &lt;code&gt;self.name&lt;/code&gt; to access the instance variable &lt;code&gt;@name&lt;/code&gt; of the current object. Using &lt;code&gt;self&lt;/code&gt; allows us to differentiate between local variables and instance variables of the object.&lt;/p&gt;




&lt;p&gt;Object-Oriented Ruby provides a powerful and flexible approach to building efficient and scalable applications. By leveraging classes, objects, instance variables and methods, class variables and methods, self, and other OOP concepts, we can organize code into reusable and modular structures. Understanding and applying these concepts allows us to write cleaner, more maintainable, and extensible code.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Building Cool Features with ReactJS</title>
      <dc:creator>Min</dc:creator>
      <pubDate>Mon, 26 Jun 2023 20:49:29 +0000</pubDate>
      <link>https://dev.to/minchulan/mastering-the-fundamentals-of-reactjs-building-features-with-elegance-1cn6</link>
      <guid>https://dev.to/minchulan/mastering-the-fundamentals-of-reactjs-building-features-with-elegance-1cn6</guid>
      <description>&lt;p&gt;ReactJS has revolutionized the way we build user interfaces, offering a component-based approach that enhances reusability, maintainability, and scalability. To effectively build out features in ReactJS, it's essential to understand the core principles that govern its design. In this blog, we'll explore the fundamentals of ReactJS and delve into the decision-making process for implementing features by considering the need for state, state placement, required props, and data passing mechanisms. Along the way, we'll provide code examples and pro tips to assist you in your React journey.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1] Understanding the Need for State:&lt;/strong&gt;&lt;br&gt;
ReactJS embraces the concept of state as a fundamental building block. State represents the data that changes over time and affects the behavior and appearance of components. When considering whether to utilize state for a feature, ask yourself: Does this feature have dynamic data that can change during runtime? If the answer is yes, state is likely necessary.&lt;/p&gt;

&lt;p&gt;Pro Tip:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Avoid overusing state and keep it minimal to maintain a clear and manageable codebase.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;2] Determining the Optimal State Placement:&lt;/strong&gt;&lt;br&gt;
Identifying where to store state within a React application is crucial for proper component design. React offers two primary options: local component state and shared state using a state management library like Redux or React Context.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Local Component State:
For features that are confined to a single component and do not require sharing data with other components, local component state is often sufficient. State is declared and managed within the component itself using the &lt;code&gt;useState&lt;/code&gt; hook, ensuring that only the relevant component has access to and controls the state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Code Example:&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;import React, { useState } from 'react';

const Counter = () =&amp;gt; {
  const [count, setCount] = useState(0);

  const increment = () =&amp;gt; {
    setCount(count + 1);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={increment}&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Counter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Shared State:
In cases where multiple components need access to the same data or when state management becomes complex, utilizing a state management library is beneficial. Redux and React Context provide mechanisms to create a shared global state accessible across components, simplifying data flow and allowing efficient updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pro Tip:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When using shared state management libraries, consider using selectors to derive data from the global state and pass it as props to individual components, reducing unnecessary re-rendering.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;3] Identifying Required Props:&lt;/strong&gt;&lt;br&gt;
Props (short for properties) are used to pass data from parent components to their children. When building features, carefully consider the data that each component needs to render correctly or perform its functionality. Determine the specific props required by each component and ensure they are defined and passed down accordingly.&lt;/p&gt;

&lt;p&gt;Pro Tip:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Destructure props in function components to improve code readability and avoid unnecessary repetition.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Code Example:&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;import React from 'react';

const UserCard = ({ name, email, role }) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h3&amp;gt;{name}&amp;lt;/h3&amp;gt;
      &amp;lt;p&amp;gt;Email: {email}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Role: {role}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default UserCard;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4] Passing Data to Components:&lt;/strong&gt;&lt;br&gt;
React offers several ways to pass data between components effectively. The choice depends on the relationship between the components and the complexity of the data being shared.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parent-to-Child Data Passing:
The most common method is passing data from a parent component to its child component using props. Parent components include the necessary data as props when rendering the child component, enabling it to access and utilize that data as needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Code Example:&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;import React from 'react';
import UserCard from './UserCard';

const UserList = () =&amp;gt; {
  const users = [
    { name: 'John Doe', email: 'john@example.com', role: 'Admin' },
    { name: 'Jane Smith', email: 'jane@example.com', role: 'User' },
  ];

  return (
    &amp;lt;div&amp;gt;
      {users.map(user =&amp;gt; (
        &amp;lt;UserCard
          key={user.email}
          name={user.name}
          email={user.email}
          role={user.role}
        /&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  );
};

export default UserList;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Child-to-Parent Data Passing:
To pass data from a child component to its parent, callbacks or event handling functions can be employed. The parent component passes a function as a prop to the child, which the child can then call, passing any relevant data as arguments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pro Tip:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Consider using the &lt;code&gt;useCallback&lt;/code&gt; hook to memoize callbacks in order to optimize performance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Code Example:&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;import React, { useState } from 'react';

const CommentBox = ({ onAddComment }) =&amp;gt; {
  const [comment, setComment] = useState('');

  const handleSubmit = () =&amp;gt; {
    onAddComment(comment);
    setComment('');
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;textarea value={comment} onChange={e =&amp;gt; setComment(e.target.value)} /&amp;gt;
      &amp;lt;button onClick={handleSubmit}&amp;gt;Add Comment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default CommentBox;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;As you can see, React allows us to build features that are efficient, maintainable, and scalable. Understanding when and where to utilize state, deciding on state placement, identifying required props, and implementing effective data passing mechanisms are key aspects of this process. Remember, React is a versatile library, and various scenarios may require different approaches.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>reactjsdevelopment</category>
    </item>
    <item>
      <title>Demystifying Active Record Associations</title>
      <dc:creator>Min</dc:creator>
      <pubDate>Wed, 10 May 2023 19:03:30 +0000</pubDate>
      <link>https://dev.to/minchulan/demystifying-active-record-associations-5p4</link>
      <guid>https://dev.to/minchulan/demystifying-active-record-associations-5p4</guid>
      <description>&lt;p&gt;As a programmer, understanding how to work with databases is crucial for building robust web applications. One fundamental concept in database management is Active Record associations. In this beginner's guide, we'll explore the world of Active Record associations, how they simplify data relationships, and how to use them effectively in your Ruby on Rails applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are Active Record Associations?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Active Record Associations define relationships between database tables/models.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;They enable you to create connections such as one-to-one, one-to-many, and many-to-many between different models.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Associations provide methods to retrieve and manipulate associated data, simplifying complex database queries.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;One-to-One Associations&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;One-to-One relationships exist when a record in one table is associated with exactly one record in another table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Explore examples like User and Profile models, where a User has one Profile.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Learn how to define a one-to-one association using the &lt;code&gt;has_one&lt;/code&gt; and &lt;code&gt;belongs_to&lt;/code&gt; methods in Rails.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# app/models/user.rb
class User &amp;lt; ApplicationRecord
  has_one :profile
end

# app/models/profile.rb
class Profile &amp;lt; ApplicationRecord
  belongs_to :user
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;One-to-Many Associations&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;One-to-Many relationships occur when a record in one table is associated with multiple records in another table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Examine scenarios like User and Post models, where a User can have multiple Posts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Discover how to define a one-to-many association using the &lt;code&gt;has_many&lt;/code&gt; and &lt;code&gt;belongs_to&lt;/code&gt; methods in Rails.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Explore additional features like dependent options (e.g., &lt;code&gt;dependent: :destroy&lt;/code&gt;) and foreign key customization.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# app/models/user.rb
class User &amp;lt; ApplicationRecord
  has_many :posts
end

# app/models/post.rb
class Post &amp;lt; ApplicationRecord
  belongs_to :user
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Many-to-Many Associations&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Many-to-Many relationships are used when multiple records in one table are associated with multiple records in another table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Discuss examples like User and Role models, where a User can have many Roles, and a Role can have many Users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Understand how to define a many-to-many association using the &lt;code&gt;has_many :through&lt;/code&gt; method in Rails.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Explore the concept of a join table and how it facilitates the relationship between models.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# app/models/user.rb
class User &amp;lt; ApplicationRecord
  has_many :user_roles
  has_many :roles, through: :user_roles
end

# app/models/role.rb
class Role &amp;lt; ApplicationRecord
  has_many :user_roles
  has_many :users, through: :user_roles
end

# app/models/user_role.rb
class UserRole &amp;lt; ApplicationRecord
  belongs_to :user
  belongs_to :role
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Querying and Eager Loading with Associations&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Learn how to retrieve associated data efficiently using methods like &lt;code&gt;joins&lt;/code&gt;, &lt;code&gt;includes&lt;/code&gt;, and &lt;code&gt;eager_load&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Understand the difference between eager loading and lazy loading.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Explore examples of querying and manipulating associated data using ActiveRecord methods.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best Practices and Common Pitfalls&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Gain insights into best practices for naming associations, table structures, and model relationships.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Discuss potential pitfalls like circular dependencies and the necessity of indexing foreign keys.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Understand the importance of testing and validating your associations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Here are some code snippets illustrating best practices and common pitfalls when working with Active Record associations:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Best Practice: Naming Conventions&lt;/u&gt;&lt;br&gt;
It's important to follow Rails' naming conventions for associations and foreign keys.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Good example
class User &amp;lt; ApplicationRecord
  has_many :posts
end

class Post &amp;lt; ApplicationRecord
  belongs_to :user
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Common Pitfall: Missing Index on Foreign Key&lt;/u&gt;&lt;br&gt;
Adding an index on foreign keys can significantly improve query performance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Migration file
class AddIndexToPosts &amp;lt; ActiveRecord::Migration[6.1]
  def change
    add_index :posts, :user_id
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Best Practice: Validations&lt;/u&gt;&lt;br&gt;
Apply necessary validations to maintain data integrity in your associations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
  has_many :posts
  validates :username, presence: true, uniqueness: true
end

class Post &amp;lt; ApplicationRecord
  belongs_to :user
  validates :title, presence: true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Common Pitfall: Inefficient Queries&lt;/u&gt;&lt;br&gt;
Avoid inefficient queries by selecting only the necessary fields when working with associations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Bad example (selecting all fields)
@users = User.includes(:posts).all
@users.each do |user|
  user.posts.each do |post|
    puts post.title
    puts post.content
    puts post.created_at
    # ...
  end
end

# Good example (selecting specific fields)
@users = User.includes(:posts).select(:id, :username)
@users.each do |user|
  user.posts.each do |post|
    puts post.title
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;As you can see, by following these best practices and avoiding common pitfalls, you'll ensure your Active Record associations are well-structured, efficient, and maintainable. Understanding how to define and utilize associations is essential for building dynamic and interconnected web applications with Ruby on Rails. By leveraging Active Record associations, you can simplify complex data relationships and unlock the full power of your database. &lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>database</category>
      <category>rails</category>
      <category>postgres</category>
      <category>sql</category>
    </item>
    <item>
      <title>The World of CRUD: Understanding HTTP Methods for Web Development</title>
      <dc:creator>Min</dc:creator>
      <pubDate>Mon, 27 Feb 2023 11:54:01 +0000</pubDate>
      <link>https://dev.to/minchulan/the-internet-is-a-load-of-crud-f6f</link>
      <guid>https://dev.to/minchulan/the-internet-is-a-load-of-crud-f6f</guid>
      <description>&lt;p&gt;When it comes to web development, understanding the Request-Response cycle and the role of HTTP methods is crucial. In this article, we'll explore the basics of the Request-Response cycle, the HTTP protocol, and the CRUD operations associated with HTTP methods. This knowledge forms the foundation for building robust web applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Request-Response Cycle&lt;/strong&gt;:&lt;br&gt;
At its core, the Request-Response cycle is the process of communication between a client (such as a browser) and a server. The client sends a request to the server, which then processes the request and generates a response. The response is then sent back to the client, where it is rendered on the browser.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP: The Language of the Web&lt;/strong&gt;:&lt;br&gt;
HTTP (HyperText Transfer Protocol) is the language used for communication between clients and servers. It enables fetching resources and exchanging data. HTTP operates within the architectural style called REST (Representational State Transfer), which provides a standardized approach to building web applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding CRUD Operations&lt;/strong&gt;:&lt;br&gt;
CRUD is an acronym for Create, Read, Update, and Delete - four fundamental operations used to manipulate data in a system. These operations align with the HTTP methods and form the basis for interacting with a relational database.&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%2F8mnjxm8dqf2fsq9ax27j.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%2F8mnjxm8dqf2fsq9ax27j.png" alt=" " width="404" height="248"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h2&gt;
  
  
  GET: Read/Retrieve
&lt;/h2&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%2Fbuas5t82rcywmaqvho81.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%2Fbuas5t82rcywmaqvho81.png" alt=" " width="800" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The GET method is used to retrieve a representation of a resource from the server. It does not modify any resources. When a client sends a GET request, the server responds with the requested data.&lt;/p&gt;




&lt;h2&gt;
  
  
  POST: Create
&lt;/h2&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%2F7wjts295ebl5kubdboj7.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%2F7wjts295ebl5kubdboj7.png" alt=" " width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The POST method is used to send data to the server to create a new resource. When a client sends a POST request, the server processes the data and creates the requested resource.&lt;/p&gt;




&lt;h2&gt;
  
  
  PATCH: Update
&lt;/h2&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%2F8v8swwq1ejv63gmly6uu.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%2F8v8swwq1ejv63gmly6uu.png" alt=" " width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The PATCH method is used to update an existing resource on the server. It involves sending data that represents the changes to be made to the resource. The server then applies those changes to the specified resource.&lt;/p&gt;




&lt;h2&gt;
  
  
  DELETE: Destroy
&lt;/h2&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%2F5p8zxp0mbohixx85gcdw.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%2F5p8zxp0mbohixx85gcdw.png" alt=" " width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The DELETE method is used to remove an existing resource from the server. When a client sends a DELETE request, the server identifies and deletes the specified resource.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h2&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%2Fha2629r6eombzkvzp6lp.png" alt=" " width="800" height="396"&gt;
&lt;/h2&gt;

&lt;p&gt;To confirm your resource has been deleted, send a &lt;code&gt;GET&lt;/code&gt; request to that specific endpoint. Your response should be &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;In web development, the Request-Response cycle, HTTP methods, and CRUD operations play a vital role. Understanding how these components work together is essential for building efficient and robust applications. The GET method retrieves data, the POST method creates new resources, the PATCH method updates existing resources, and the DELETE method removes resources. &lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>http</category>
      <category>database</category>
      <category>crud</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Inverse Data Flow in React: Updating Parent from Child Components with Callback Functions</title>
      <dc:creator>Min</dc:creator>
      <pubDate>Mon, 19 Dec 2022 06:18:48 +0000</pubDate>
      <link>https://dev.to/minchulan/passing-data-from-child-to-parent-component-using-functions-305d</link>
      <guid>https://dev.to/minchulan/passing-data-from-child-to-parent-component-using-functions-305d</guid>
      <description>&lt;p&gt;In React, data typically flows down from parent to child components through props. However, there may be scenarios where we need to update the parent component when changes occur in the child component. In this tutorial, we'll explore inverse data flow in React and learn how to update the parent component by using callback functions. We'll build a simple example where a button click in the child component updates the state and changes the heading in the parent component.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Setting up the Components&lt;/strong&gt;:&lt;br&gt;
We'll create two components: &lt;code&gt;Parent&lt;/code&gt; and &lt;code&gt;Child&lt;/code&gt;. The &lt;code&gt;Parent&lt;/code&gt; component will render the &lt;code&gt;Child&lt;/code&gt; component and display a heading. The &lt;code&gt;Child&lt;/code&gt; component will have a button that triggers the state update in the parent 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%2Fgcsy5j2u1x21soldluuc.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%2Fgcsy5j2u1x21soldluuc.png" alt=" " width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Parent.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import Child from './Child';

const Parent = () =&amp;gt; {
   const [header, setHeader] = useState('I am a Parent');

   return (
     &amp;lt;div className="parent"&amp;gt;
        &amp;lt;h1&amp;gt;{header}&amp;lt;/h1&amp;gt;
        &amp;lt;Child handleClick={(newHeader) =&amp;gt; setHeader(newHeader)} /&amp;gt;
     &amp;lt;/div&amp;gt;
   );
}

export default Parent;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Child.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';

const Child = (props) =&amp;gt; {
    return (
      &amp;lt;div className="child"&amp;gt;
        &amp;lt;h1&amp;gt;I am a Child&amp;lt;/h1&amp;gt;
        &amp;lt;button onClick={() =&amp;gt; props.handleClick("I am a Changed Parent")}&amp;gt;Click&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
}

export default Child;

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

&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;In the &lt;code&gt;Parent&lt;/code&gt; component, we use the &lt;code&gt;useState&lt;/code&gt; hook to create a state variable called &lt;code&gt;header&lt;/code&gt; and its corresponding setter function, &lt;code&gt;setHeader&lt;/code&gt;. The initial value of &lt;code&gt;header&lt;/code&gt; is set to "I am a Parent".&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Parent&lt;/code&gt; component renders the &lt;code&gt;Child&lt;/code&gt; component and passes a callback function &lt;code&gt;handleClick&lt;/code&gt; as a prop.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;Child&lt;/code&gt; component, we have a button with an &lt;code&gt;onClick&lt;/code&gt; event that invokes the &lt;code&gt;handleClick&lt;/code&gt; function passed from the parent. When the button is clicked, it triggers the callback function and passes the new &lt;code&gt;header&lt;/code&gt; value as an argument.&lt;/li&gt;
&lt;li&gt;Inside the callback function in the &lt;code&gt;Parent&lt;/code&gt; component, we update the state by calling &lt;code&gt;setHeader&lt;/code&gt; with the new &lt;code&gt;header&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;The updated state causes a re-render, and the new &lt;code&gt;header&lt;/code&gt; value is displayed in the parent component.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Inverse data flow in React allows us to update the parent component when changes occur in the child component. By passing callback functions as props from the parent to the child, we can modify the state in the parent component based on child interactions. This approach allows for better separation of concerns and modularization of components, as the child component doesn't directly modify the parent's state but instead notifies the parent to handle the state update. Understanding this concept is essential for managing data and state across different components in a React application. &lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;




&lt;p&gt;Resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/thinking-in-react.html" rel="noopener noreferrer"&gt;Thinking in React&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a&gt;React Interactivity: Events and State&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/lifting-state-up.html" rel="noopener noreferrer"&gt;Lifting State Up&lt;/a&gt; &lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript Arrays: Exploring Methods and Immutability</title>
      <dc:creator>Min</dc:creator>
      <pubDate>Thu, 29 Sep 2022 23:57:45 +0000</pubDate>
      <link>https://dev.to/minchulan/javascript-the-tldr-on-array-methods-1el5</link>
      <guid>https://dev.to/minchulan/javascript-the-tldr-on-array-methods-1el5</guid>
      <description>&lt;p&gt;Arrays are a fundamental data structure in JavaScript that allow us to store and manipulate collections of elements. In this article, we'll explore arrays, learn about accessing elements by index, and delve into some commonly used array methods. Additionally, we'll discuss array mutability and techniques to create immutable arrays.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Creating and Accessing Elements in Arrays&lt;/strong&gt;:&lt;br&gt;
Arrays are created using square brackets and can hold various data types, including numbers, strings, booleans, functions, and even other arrays. Each element in an array is assigned an index, with the first element starting at index 0. To access elements in an array, we use bracket notation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// An array containing numbers
const numberArray = [1, 2, 3, 4, 5];

// An array containing strings
const stringArray = ['one', 'two', 'three', 'four', 'five'];
&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;// Accessing elements in the array
console.log(stringArray[0]); 
// "one"
console.log(stringArray[1]); 
// "two"
console.log(stringArray[2]); 
// "three"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Commonly Used Array Methods&lt;/strong&gt;:&lt;br&gt;
JavaScript provides several built-in methods that make working with arrays more convenient and efficient. Let's explore three useful array methods: &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, and &lt;code&gt;forEach&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;1) &lt;code&gt;map&lt;/code&gt;: This method transforms each element in an array and returns a new array with the modified elements. It executes a callback function on each item and replaces it with the result.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5] 
numbers.map(num =&amp;gt; num * 2); 
// [2, 4, 6, 8, 10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) &lt;code&gt;filter&lt;/code&gt;: The &lt;code&gt;filter&lt;/code&gt; method creates a new array containing elements that satisfy a given condition. It executes a callback function on each item and returns a new array with only the elements that meet the condition.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const movies = [
   {
      title: 'Golden Eye',
      category: 'Thriller',
      rating: 95,
      year: 2010
   },
   {
      title: 'The Avengers',
      category: 'Action',
      rating: 98,
      year: 2020
   },
   {
      title: 'Love Actually',
      category: 'Romantic comedy',
      rating: 85,
      year: 1998
   },

]


movies.filter(movie =&amp;gt; movie.title); 
// [{title: 'Golden Eye'}, {title: 'The Avengers'}, {title: 'Love Actually'}]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) &lt;code&gt;forEach&lt;/code&gt;: The &lt;code&gt;forEach&lt;/code&gt; method executes a callback function on each element in an array. It is useful for performing operations on each item without creating a new array.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5];
numbers.forEach(number =&amp;gt; console.log(number); 
// [1, 2, 3, 4, 5];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Array Mutability and Immutable Techniques&lt;/strong&gt;:&lt;br&gt;
Arrays in JavaScript are mutable, meaning their elements can be changed. However, we can create a copy of an array to preserve the original data while making modifications. The spread operator (...) can be used to create a shallow copy of an array.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3];
const newArray = [...array, 4];
console.log(newArray); // [1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;As you can see, arrays are a powerful data structure in JavaScript that allow us to store and manipulate collections of elements. We learned how to create arrays, access elements by index, and explored some commonly used array methods like &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, and &lt;code&gt;forEach&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's important to understand array mutability. Although arrays are mutable by default, we can create immutable arrays by making copies of the original array using techniques like the spread operator (...). This allows us to modify the copied content while leaving the original array unchanged. Alternatively, &lt;code&gt;Object.freeze()&lt;/code&gt; can be used to make the array itself immutable, but not its contents.&lt;/p&gt;

&lt;p&gt;By leveraging arrays and their methods, we can efficiently manipulate and transform data, making our code more readable and maintainable. Understanding arrays and their methods is essential for effective JavaScript programming.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;&lt;/p&gt;

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