<?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: brvarner</title>
    <description>The latest articles on DEV Community by brvarner (@brvarner).</description>
    <link>https://dev.to/brvarner</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%2F1273308%2F3a6485bc-4e64-4ae1-a845-7629c52f7b5b.jpeg</url>
      <title>DEV Community: brvarner</title>
      <link>https://dev.to/brvarner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brvarner"/>
    <language>en</language>
    <item>
      <title>Rails and Postgres</title>
      <dc:creator>brvarner</dc:creator>
      <pubDate>Thu, 06 Jun 2024 21:10:09 +0000</pubDate>
      <link>https://dev.to/brvarner/rails-and-postgres-5234</link>
      <guid>https://dev.to/brvarner/rails-and-postgres-5234</guid>
      <description>&lt;p&gt;I'm slowly grasping the connection between Rails and Postgres. I'm starting to feel like this is the key to the Model-View-Controller system, but I'm still working on fully wrapping my head around this bad boy.&lt;/p&gt;

&lt;p&gt;Luckily, I took copious notes during most Postgres/Rails lesson, and you can &lt;a href="https://github.com/brvarner/dpi-notes/blob/main/june5.md"&gt;read them here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;You must first download &lt;a href="https://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt;, and follow the prompts to learn its console line commands.&lt;/p&gt;

&lt;p&gt;You must start by initializing a database and then creating a table. &lt;/p&gt;

&lt;p&gt;When you create a table, you must include the fields and data types that all of your entries will have. Here's an example from the exercise in the course, where we created a contact book:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE contacts (
  id SERIAL PRIMARY KEY,
  first_name TEXT,
  last_name TEXT,
  date_of_birth DATE,
  street_address_1 TEXT,
  street_address_2 TEXT,
  city TEXT,
  state TEXT,
  zip TEXT,
  phone TEXT,
  notes TEXT,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The id field will always be a SERIAL PRIMARY KEY to indicate it should auto-increment and that this will be the primary key of your table. &lt;/p&gt;

&lt;p&gt;After creating your table, you can launch it to prime it for editing/access with the psql command, or by using rails db. &lt;/p&gt;

&lt;p&gt;To link your Rails project with your database, you have to edit your &lt;code&gt;database.yaml&lt;/code&gt; file. Change &lt;code&gt;rails_template_development&lt;/code&gt; on line 27 to your database's name and it'll find it on your computer. &lt;/p&gt;

&lt;p&gt;Also, you can access Rails' built-in database GUI by launching bin/server and navigating to &lt;code&gt;rails/db&lt;/code&gt; in your project. From there, you can perform CRUD operations on data or observe.&lt;/p&gt;

&lt;h3&gt;
  
  
  Models
&lt;/h3&gt;

&lt;p&gt;You must interface with your database via a Model, which Rails provides as an easy way to structure your database actions. Models are Ruby classes that "talk to the database, store and validate data, perform the business logic, and otherwise do the heavy lifting".&lt;/p&gt;

&lt;p&gt;These models inherit methods from Rails's built-in ActiveRecord class, which lets them do many different moves. Each model represents a single row of data, so they're named singularly after the name of the table (i.e. a model named Contact for a table named contacts). This naming convention helps Rails automatically locate the table that the model represents.&lt;/p&gt;

&lt;p&gt;We can operate on the data several ways once we complete setup, but I'm calling it on Rails/Postgres until we do it next week as a class. &lt;/p&gt;

</description>
      <category>rails</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Database Design</title>
      <dc:creator>brvarner</dc:creator>
      <pubDate>Thu, 30 May 2024 21:59:57 +0000</pubDate>
      <link>https://dev.to/brvarner/database-design-5g4j</link>
      <guid>https://dev.to/brvarner/database-design-5g4j</guid>
      <description>&lt;p&gt;Today we focused on database design, specifically exploring the concepts that drive relational databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's a relational database?
&lt;/h2&gt;

&lt;p&gt;It's a collection of tables storing different data pieces that refer to one another to form a complete picture of something. By referencing other tables, we can stick to one value per field design principles while adding organized complexity to our entries.&lt;/p&gt;

&lt;h3&gt;
  
  
  What?
&lt;/h3&gt;

&lt;p&gt;Today, we used a film database as an example. This database had tables for films, actors, and directors. &lt;/p&gt;

&lt;p&gt;You can't just include a list of every actor in a movie in the films table, so you need a way to keep track of each film's cast. This is where a one-to-many join table comes into play. &lt;/p&gt;

&lt;p&gt;You can create a table where each record is a film ID, an actor's ID, and the name of the role they played. A query to this table can then return each actor ID matched with a single film's ID and provide a comprehensive list of every actor in that film.&lt;/p&gt;

&lt;p&gt;This way, you can keep track of the dozens of actors that make up a film's cast without bogging down every table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This form of database design applies to SQL, it's been a while since I've studied SQL, so it was nice to get a refresher.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
