<?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: Carisa Elam </title>
    <description>The latest articles on DEV Community by Carisa Elam  (@carisaelam).</description>
    <link>https://dev.to/carisaelam</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%2F1427517%2Fd608978d-1927-4704-8c2b-8bea5aa6e803.jpeg</url>
      <title>DEV Community: Carisa Elam </title>
      <link>https://dev.to/carisaelam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/carisaelam"/>
    <language>en</language>
    <item>
      <title>Active Record Queries Cheat Sheet</title>
      <dc:creator>Carisa Elam </dc:creator>
      <pubDate>Fri, 16 Aug 2024 23:29:48 +0000</pubDate>
      <link>https://dev.to/carisaelam/active-record-queries-cheat-sheet-3ppi</link>
      <guid>https://dev.to/carisaelam/active-record-queries-cheat-sheet-3ppi</guid>
      <description>&lt;p&gt;Active Record allows you to execute SQL commands using Ruby. This guide cuts through the fluff to provide &lt;strong&gt;&lt;em&gt;a simple reference for some common Active Record queries&lt;/em&gt;&lt;/strong&gt;. For each query, you’ll find the SQL equivalent and a brief description of what it returns, making it easy to see how the methods map to SQL commands.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;code&gt;find&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL Equivalent:&lt;/strong&gt; &lt;code&gt;SELECT * FROM model WHERE (model.id = :id)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Returns:&lt;/strong&gt; the record with the specific ID.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;code&gt;take&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL Equivalent:&lt;/strong&gt; &lt;code&gt;SELECT * FROM model LIMIT 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Returns:&lt;/strong&gt; the record without any ordering&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;code&gt;first&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL Equivalent:&lt;/strong&gt; &lt;code&gt;SELECT * FROM model ORDER BY model.id ASC LIMIT 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Returns:&lt;/strong&gt; the record with the first primary key that exists&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;code&gt;last&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL Equivalent:&lt;/strong&gt; &lt;code&gt;SELECT * FROM model ORDER BY model.id DESC LIMIT 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Returns:&lt;/strong&gt; the record with the last primary key that exists&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;code&gt;find_by&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL Equivalent:&lt;/strong&gt; &lt;code&gt;SELECT * FROM model WHERE (model.parameter = 'foo') LIMIT 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Returns:&lt;/strong&gt; the first record matching the condition&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;em&gt;This is not meant to be an exhaustive guide.&lt;/em&gt; &lt;br&gt;
For more information, see the &lt;a href="https://guides.rubyonrails.org/active_record_querying.html#what-is-the-active-record-query-interface-questionmark" rel="noopener noreferrer"&gt;Rails Guides: Active Record Query Interface&lt;/a&gt;. &lt;/p&gt;




&lt;p&gt;  &lt;br&gt;
👋&lt;strong&gt;Connect with me on &lt;a href="https://github.com/carisaelam" rel="noopener noreferrer"&gt;Github&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/carisa-elam-097368239" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;. &lt;br&gt;
I'd love to hear your thoughts!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>rails</category>
      <category>ruby</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Beginner–Friendly Basics: Rails Migrations</title>
      <dc:creator>Carisa Elam </dc:creator>
      <pubDate>Tue, 13 Aug 2024 20:03:16 +0000</pubDate>
      <link>https://dev.to/carisaelam/beginner-friendly-basics-rails-migrations-n61</link>
      <guid>https://dev.to/carisaelam/beginner-friendly-basics-rails-migrations-n61</guid>
      <description>&lt;p&gt;If you are new to Rails, understanding migrations can feel overwhelming. This article covers the basics and simplifies some of the primary components of migration. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We'll use a super simple &lt;a href="https://odin-blog-app-production.up.railway.app" rel="noopener noreferrer"&gt;blog app&lt;/a&gt; deployed via &lt;a href="https://railway.app?referralCode=iwPZcx" rel="noopener noreferrer"&gt;Railway&lt;/a&gt; as an example throughout this article.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;h3&gt;
  
  
  What is a Rails Migration?
&lt;/h3&gt;

&lt;p&gt;Put simply, it is just a script that updates the schema of a database.&lt;/p&gt;

&lt;p&gt;Each migration is like an updated version of your database.&lt;/p&gt;

&lt;p&gt;When a Rails project is created, often one of the first things you need to do is to start creating a data model. In our case, a model for the articles that will populate the blog. Because this process involves the creation of a database table, you need to use migration to "change the schema" by setting up the table.&lt;/p&gt;

&lt;p&gt;Rails migration commands are simple and plain English, but they translate into SQL queries and get the job done under the hood.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  How Do You Perform a Migration?
&lt;/h3&gt;

&lt;p&gt;Migrations can be triggered in a handful of different ways. We'll discuss the main two: &lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Generating a model&lt;/li&gt;
&lt;li&gt;Generating a standalone migration&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;&lt;strong&gt;1. Generating a Model&lt;/strong&gt;&lt;br&gt;
No need to create all of the files and connections yourself, Rails handles most of the legwork for you.&lt;/p&gt;

&lt;p&gt;For our blog example, running this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails generate model Article
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;would return something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails generate model Article
      invoke  active_record
      create    db/migrate/20240812123456_create_articles.rb
      create    app/models/article.rb
      invoke    test_unit
      create      test/models/article_test.rb
      create      test/fixtures/articles.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break that down.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;invoke active_record&lt;/code&gt;&lt;/strong&gt;: as it implies, this brings Active Record into the picture&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;create    db/migrate/20240812123456_create_articles.rb&lt;/code&gt;&lt;/strong&gt;: generates a migration file with a class named &lt;code&gt;CreateArticles&lt;/code&gt;, which will ultimately create the &lt;code&gt;articles&lt;/code&gt; table.&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;CreateArticles&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;7.2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="ss"&gt;:articles&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timestamps&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;&lt;strong&gt;&lt;code&gt;create    app/models/article.rb&lt;/code&gt;&lt;/strong&gt;: creates your model&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;Article&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="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...and the model creates concludes by generating some testing files. &lt;/p&gt;

&lt;p&gt; &lt;br&gt;
&lt;strong&gt;2. Generating a standalone Migration&lt;/strong&gt;&lt;br&gt;
When you need to modify an existing data table or create a new schema change WITHOUT creating a new model, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails generate migration CreateArticle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which will output something like this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails generate migration CreateArticle
      invoke  active_record
      create    db/migrate/20240812123456_create_article.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, this command results in fewer things happening. Active Record is invoked, and an article migration file is created. &lt;code&gt;rails generate migration&lt;/code&gt; does just what it says—it creates a migration file, not a model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When would I need to use &lt;code&gt;rails generate migration&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adding or removing columns from existing database tables&lt;/li&gt;
&lt;li&gt;Renaming a column or a table&lt;/li&gt;
&lt;li&gt;Changing the data type of a column&lt;/li&gt;
&lt;li&gt;Creating a join table&lt;/li&gt;
&lt;li&gt;Deleting ('dropping') a table from the database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Am I done?&lt;/strong&gt;&lt;br&gt;
No.&lt;br&gt;
At this point, our changes have not actually been applied to the database schema. To do that, you would run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;and voila! Your database table has been created, updated, deleted, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do I know the migration was successful?&lt;/strong&gt;&lt;br&gt;
Upon completion, the migration should output something like this to confirm success:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;==  CreateArticles: migrating =================================================
-- create_table(:articles)
   -&amp;gt; 0.0030s
==  CreateArticles: migrated (0.0030s) ========================================

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What if I mess up something?&lt;/strong&gt;&lt;br&gt;
Made a mistake? No problem. Migrations are typically reversible, and it's almost as easy as &lt;code&gt;Ctrl–Z&lt;/code&gt;! You simply run&lt;br&gt;
&lt;code&gt;$ rails db:rollback&lt;/code&gt; and your migration effectively gets "undone." There's more to it than that under the hood, but as long as you have specified how each method should "reverse" itself, Rails handles the rest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do I need to reserve the first column for a primary key?&lt;/strong&gt;&lt;br&gt;
Typically, the first column in a table is reserved for the &lt;code&gt;id&lt;/code&gt;, which serves as a unique identifier for each row of the table. By default, Rails automatically includes an auto-incrementing primary key &lt;code&gt;id&lt;/code&gt; column to each table. So no need to do that yourself! You won't really "see" that anywhere in the code, you just have to hop on the Rails train and believe that it is happening behind the scenes.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  What Else Can Migrations Do?
&lt;/h3&gt;

&lt;p&gt;Migrations are used in a ton of processes within a Rails app. While this article covers just the basics, here are some other things you can do with migrations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create join tables&lt;/li&gt;
&lt;li&gt;Change current tables&lt;/li&gt;
&lt;li&gt;Change current columns&lt;/li&gt;
&lt;li&gt;Add column modifiers (&lt;code&gt;comment&lt;/code&gt;, &lt;code&gt;default&lt;/code&gt;, &lt;code&gt;limit&lt;/code&gt;, etc.)&lt;/li&gt;
&lt;li&gt;Create foreign key columns to connect tables&lt;/li&gt;
&lt;li&gt;Execute custom SQL commands (for when the built-in functions just aren't enough)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;While we've just scratched the surface of what migrations can do, it's clear that Rails migrations can simplify the process of updating your database. Like &lt;a href="https://dev.to/carisaelam/active-record-quick-start-46o5"&gt;Active Record&lt;/a&gt;, migrations are designed to be as user-friendly as possible for developers while all of the complexity is handled behind the scenes. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The result? We can get more done and focus on creating, not configuring.&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

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

&lt;h3&gt;
  
  
  Helpful Links
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.theodinproject.com" rel="noopener noreferrer"&gt;The Odin Project&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/carisaelam/active-record-quick-start-46o5"&gt;Rails Active Record: Quick Start!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://guides.rubyonrails.org/active_record_migrations.html" rel="noopener noreferrer"&gt;Rails Guide: Active Record Migrations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Image by &lt;a href="https://www.freepik.com/free-vector/hand-drawn-website-hosting-illustration_22112058.htm#fromView=search&amp;amp;page=1&amp;amp;position=3&amp;amp;uuid=0bd2748f-23e7-44a0-8a81-0a164c87b165" rel="noopener noreferrer"&gt;freepik&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;  &lt;br&gt;
👋&lt;strong&gt;Connect with me on &lt;a href="https://github.com/carisaelam" rel="noopener noreferrer"&gt;Github&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/carisa-elam-097368239" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;. &lt;br&gt;
I'd love to hear your thoughts!&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Rails Active Record: Quick Start!</title>
      <dc:creator>Carisa Elam </dc:creator>
      <pubDate>Mon, 12 Aug 2024 21:04:56 +0000</pubDate>
      <link>https://dev.to/carisaelam/active-record-quick-start-46o5</link>
      <guid>https://dev.to/carisaelam/active-record-quick-start-46o5</guid>
      <description>&lt;h4&gt;
  
  
  👋 Hello fellow learner!
&lt;/h4&gt;

&lt;p&gt;  &lt;br&gt;
As I dive into Rails for the first time with the Odin Project, I’m learning just how cool (and a bit confusing) Active Record can be. I decided to write this post to help my future self remember the basics and to share what I’ve learned with others. Think of it as a quick reference to getting started with Active Record without the fuss. It highlights a few elements of Active Record that I thought were particularly important. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hope this helps clear things up and makes your Rails journey a bit smoother!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  What is Active Record?
&lt;/h3&gt;

&lt;p&gt;Active Record is the part of Rails that handles the database and connects it to your application. Creating tables, altering schemas, and formulating queries requires a certain level of complexity and a lot of configuration code. Rails handles all of that for us with Active Record and &lt;strong&gt;simplifies the process of interacting with our database&lt;/strong&gt;. It is almost like a translator between Ruby and basically any database language. &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What does Active Record do?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Takes the app data from the rows and columns in your database&lt;/li&gt;
&lt;li&gt;Allows you to interact with the data as a Ruby object&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is like magic!—like a little middleman between you and your database that handles writing and executing all of the queries you would otherwise have to type out yourself. Instead, you provide it with something written in Ruby (which is much easier to write, read, and understand, IMHO), and it translates that into the language of your database and returns the result you're looking for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;So how does Active Record know what kind of database you are using?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
You basically give Active Record some basic information about your database via the &lt;code&gt;config/database.yml&lt;/code&gt; file, and it handles the rest. That means if you end up switching databases later on, you should theoretically only have to change the configuration info. Is this what they mean by 🚃 &lt;em&gt;The Rails Way&lt;/em&gt; 🚃? &lt;/p&gt;

&lt;p&gt;It seems pretty nice honestly...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;How does this look on a Rails app?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
We'll use a super simple &lt;a href="https://odin-blog-app-production.up.railway.app" rel="noopener noreferrer"&gt;blog app&lt;/a&gt; deployed via &lt;a href="https://railway.app?referralCode=iwPZcx" rel="noopener noreferrer"&gt;Railway&lt;/a&gt; as an example.&lt;/p&gt;

&lt;p&gt;Information about the articles on the blog are stored in a database table called &lt;code&gt;articles&lt;/code&gt;. The model used to access that data is called &lt;code&gt;article&lt;/code&gt;, in keeping with the &lt;a href="https://dev.to/scrabill/singular-or-plural-a-cheatsheet-for-ruby-on-rails-generators-4cb8"&gt;Rails singular/plural configuration&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The model is simply a Ruby object...&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;Article&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="o"&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;...that inherits from &lt;code&gt;ApplicationRecord&lt;/code&gt; which in turn inherits from &lt;code&gt;ActiveRecord&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationRecord&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;primary_abstract_class&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 there's the connection! And it is easy to see thanks to Ruby.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Naming conventions
&lt;/h3&gt;

&lt;p&gt;This isn't just to make your code look pretty. Rails emphasizes &lt;em&gt;"convention over configuration"&lt;/em&gt; to help lighten the load on developers. Instead of having to write out lots of lines of configuration code to get your application to work, just hop onboard the Rails wagon 🚃 and do it like they say to.&lt;/p&gt;

&lt;p&gt;Rails will automatically pluralize your model's class names. In our case, the model was &lt;code&gt;class Article&lt;/code&gt;, so the database table is called &lt;code&gt;articles&lt;/code&gt; as you can see in our &lt;code&gt;articles&lt;/code&gt; 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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ArticlesController&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;@articles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&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;show&lt;/span&gt;
    &lt;span class="vi"&gt;@article&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&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;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Rails uses the &lt;code&gt;Active Support&lt;/code&gt; library to accomplish this. So it is not magic, nor is it simply adding an 's' on at the end. (e.g., &lt;code&gt;Person&lt;/code&gt; gets mapped to &lt;code&gt;people&lt;/code&gt; automatically.)&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Creating an Active Record model
&lt;/h3&gt;

&lt;p&gt;When you create a new Rails app and generate your first model, the &lt;code&gt;ApplicationRecord&lt;/code&gt; class is created and placed in the &lt;code&gt;app/models&lt;/code&gt; directory. As we saw before, &lt;code&gt;ApplicationRecord&lt;/code&gt; inherits from &lt;code&gt;ActiveRecord::Base&lt;/code&gt;. So to create a new Active Record model, you just create a new class that inherits from &lt;code&gt;Application Record&lt;/code&gt;. Remember to make your class name singular!&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;Article&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="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you do this, Rails creates a &lt;code&gt;Article&lt;/code&gt; model that is mapped to a &lt;code&gt;articles&lt;/code&gt; table in the database. Each column of the table will be mapped to an attribute of the &lt;code&gt;Article&lt;/code&gt; class. An instance of the &lt;code&gt;Article&lt;/code&gt; class represents a single row in the &lt;code&gt;articles&lt;/code&gt; database table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;But where is the table? Is it created already?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Rather than creating the table via a &lt;code&gt;CREATE TABLE&lt;/code&gt; SQL statement that explicitly defines all of the column headers and their types, you just use an Active Record migration on the command line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ bin/rails generate migration CreateArticles title:string body:text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;CreateArticles&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;7.2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="ss"&gt;:articles&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:title&lt;/span&gt;
      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt; &lt;span class="ss"&gt;:body&lt;/span&gt;

      &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timestamps&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ bin/rails db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another example of hopping on the Rails wagon 🚃 and letting it do its thing.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  CRUD and Active Record
&lt;/h3&gt;

&lt;p&gt;Rails is built around the idea that most applications operate on data in four ways: &lt;strong&gt;C&lt;/strong&gt;reating, &lt;strong&gt;R&lt;/strong&gt;eading, &lt;strong&gt;U&lt;/strong&gt;pdating, and &lt;strong&gt;D&lt;/strong&gt;eleting (CRUD). This is so ingrained in Rails that when you create a new Active Record, Rails automatically provides a &lt;a href="https://guides.rubyonrails.org/active_record_querying.html" rel="noopener noreferrer"&gt;set of built-in methods&lt;/a&gt; to make it super easy to perform CRUD operations.&lt;/p&gt;

&lt;p&gt;Each of these methods results in the creation and execution of SQL statements behind the scenes. Rails also provides plain English ways of accessing these methods, making it &lt;em&gt;even easier&lt;/em&gt; to connect with your records. 🚃 Hop Aboard!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What you see: &lt;code&gt;articles = Article.all&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What goes on behind the scenes: &lt;code&gt;SELECT "articles".* FROM "articles"&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Active Record is a fantastic tool, though it can be confusing when you're first learning it. But the takeaway is that Active Record handles a lot of the heavy lifting, syntax, and configuration, allowing us to interact with our database via Ruby objects. When in doubt, hop on board the Ruby wagon 🚃 and let Active Record do what it is meant to do: make our lives easier as developers.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Helpful Links
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.theodinproject.com" rel="noopener noreferrer"&gt;The Odin Project&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;a href="https://guides.rubyonrails.org/active_record_basics.html" rel="noopener noreferrer"&gt;Rails Guides: Active Record Basics&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@sashbo70?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;Sash Bo&lt;/a&gt;  on &lt;a href="https://unsplash.com/photos/a-train-traveling-down-train-tracks-next-to-a-forest-beuDQp9XOp0?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;  &lt;br&gt;
👋&lt;strong&gt;Connect with me on &lt;a href="https://github.com/carisaelam" rel="noopener noreferrer"&gt;Github&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/carisa-elam-097368239" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;. &lt;br&gt;
I'd love to hear your thoughts!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>tutorial</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
