<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: grant-ours</title>
    <description>The latest articles on Forem by grant-ours (@grantours).</description>
    <link>https://forem.com/grantours</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%2F883680%2Ffb13bcc7-5c4e-4659-90c3-04fc578358d4.png</url>
      <title>Forem: grant-ours</title>
      <link>https://forem.com/grantours</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/grantours"/>
    <language>en</language>
    <item>
      <title>Active Record Validations</title>
      <dc:creator>grant-ours</dc:creator>
      <pubDate>Wed, 26 Oct 2022 21:55:42 +0000</pubDate>
      <link>https://forem.com/grantours/active-record-validations-17cf</link>
      <guid>https://forem.com/grantours/active-record-validations-17cf</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is a Validation?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's an example.&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
  validates :name, presence: true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; User.create(name: "John Doe").valid?
=&amp;gt; true
irb&amp;gt; User.create(name: nil).valid?
=&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The validation lets us know that our User is not valid without a name attribute. The second User will not be persisted to the database because it did not pass validation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Use Validation?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Validations are used to make sure that only valid data is saved into your database. For example, it may be important in your application to ensure that every user provides a valid username and password. Model-level validations are the best way to ensure that only valid data is saved into your database. Validations cannot be bypassed by end users and are convenient to test and maintain. Rails provides built-in helpers for common needs, and allows you to create your own validation methods as well but I wont be covering all that here.&lt;/p&gt;

&lt;p&gt;There are several other ways to validate data before it is saved into your database, including native database constraints, client-side validations and controller-level validations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When Does Validation Happen?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two kinds of Active Record objects: those that correspond to a row inside your database and those that do not. When you create a new object, for example using the &lt;em&gt;new&lt;/em&gt; method, that object does not belong to the database yet. Once you call save on that object it will be saved into the appropriate database table. Active Record uses the &lt;em&gt;new_record?&lt;/em&gt; instance method to determine whether an object is already in the database or not. In this example we have an Active Record class with no validations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; u = User.new(username: "Johnny")
=&amp;gt; #&amp;lt;User id: nil, username: "Johnny", created_at: nil, updated_at: nil&amp;gt;

irb&amp;gt; u.new_record?
=&amp;gt; true

irb&amp;gt; u.save
=&amp;gt; true

irb&amp;gt; u.new_record?
=&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating and saving a new record will send an SQL INSERT operation to the database. Updating an existing record will send an SQL UPDATE operation instead. Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not perform the INSERT or UPDATE operation. This prevents us from storing an invalid object in the database. You can choose to have specific validations run when an object is created, saved, or updated.&lt;/p&gt;

&lt;p&gt;The following methods trigger validations, and will save the object to the database only if the object is valid:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;create&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;create!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;save&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;save!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;update&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;update!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The bang operator ("!") at the end of the method raises an exception if the record is invalid. The non-bang versions don't: save and update return false, and create returns the object.&lt;/p&gt;

&lt;p&gt;The following methods skip validations, and will save the object to the database whether it's valid or not. They should be used carefully.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;decrement!&lt;/li&gt;
&lt;li&gt;decrement_counter&lt;/li&gt;
&lt;li&gt;increment!&lt;/li&gt;
&lt;li&gt;increment_counter&lt;/li&gt;
&lt;li&gt;insert&lt;/li&gt;
&lt;li&gt;insert!&lt;/li&gt;
&lt;li&gt;insert_all&lt;/li&gt;
&lt;li&gt;insert_all!&lt;/li&gt;
&lt;li&gt;toggle!&lt;/li&gt;
&lt;li&gt;touch&lt;/li&gt;
&lt;li&gt;touch_all&lt;/li&gt;
&lt;li&gt;update_all&lt;/li&gt;
&lt;li&gt;update_attribute&lt;/li&gt;
&lt;li&gt;update_column&lt;/li&gt;
&lt;li&gt;update_columns&lt;/li&gt;
&lt;li&gt;update_counters&lt;/li&gt;
&lt;li&gt;upsert&lt;/li&gt;
&lt;li&gt;upsert_all&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  valid? and invalid?
&lt;/h2&gt;

&lt;p&gt;Before saving an Active Record object, Rails runs your validations. If these validations produce any errors, Rails does not save the object.&lt;/p&gt;

&lt;p&gt;You can also run these validations on your own. valid? triggers your validations and returns true if no errors were found in the object, and false otherwise. invalid? is the inverse of valid?. It triggers your validations, returning true if any errors were found in the object, and false otherwise.&lt;/p&gt;

&lt;p&gt;There are many helper methods and different ways to deal with and display error messages but I wont delve into those here.  Feel free to check them out here &lt;a href="https://guides.rubyonrails.org/active_record_validations.html"&gt;https://guides.rubyonrails.org/active_record_validations.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>react</category>
    </item>
    <item>
      <title>Many-to-Many Relationships</title>
      <dc:creator>grant-ours</dc:creator>
      <pubDate>Mon, 10 Oct 2022 21:34:49 +0000</pubDate>
      <link>https://forem.com/grantours/many-to-many-relationships-4p90</link>
      <guid>https://forem.com/grantours/many-to-many-relationships-4p90</guid>
      <description>&lt;h2&gt;
  
  
  Many-to-many relationships
&lt;/h2&gt;

&lt;p&gt;are one of the more common relationships in Rails. When two models have a has_many association to each other, we say they are in a many-to-many relationship.&lt;/p&gt;

&lt;p&gt;Here's a many-to-many relationship for example, a doctor can have many patients, and a patient can see many different doctors.&lt;/p&gt;

&lt;p&gt;One way to set up a many-to-many relationship with another model is to use the &lt;code&gt;has_many :through&lt;/code&gt; association. In order to do that, we need to create a new join model. For this example we will use Doctor, Patient and Appointment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Doctor &amp;lt; ApplicationRecord
  has_many :appointments
  has_many :patients, through: :appointments
end

class Appointment &amp;lt; ApplicationRecord
  belongs_to :doctor
  belongs_to :patient
end

class Patient &amp;lt; ApplicationRecord
  has_many :appointments
  has_many :doctors, through: :appointments
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But how can we control the associated object when its owner is destroyed?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can set a &lt;em&gt;dependent&lt;/em&gt; to handle these conditions.&lt;/p&gt;

&lt;p&gt;For example if I wanted to delete a Doctor I'd need to make sure all of their appointments were taken care of.  We'd do that 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;class Doctor &amp;lt; ApplicationRecord
  has_many :appointments, dependent: :destroy
  has_many :patients, through: :appointments
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more on active record associations check out: &lt;a href="https://guides.rubyonrails.org/association_basics.html"&gt;https://guides.rubyonrails.org/association_basics.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Ruby Methods</title>
      <dc:creator>grant-ours</dc:creator>
      <pubDate>Mon, 19 Sep 2022 07:08:08 +0000</pubDate>
      <link>https://forem.com/grantours/ruby-methods-10i4</link>
      <guid>https://forem.com/grantours/ruby-methods-10i4</guid>
      <description>&lt;h2&gt;
  
  
  What Are Methods
&lt;/h2&gt;

&lt;p&gt;Ruby methods are like functions in other programming languages. Ruby methods are used to group repeatable code into a single reusable &lt;em&gt;methods&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Method names should &lt;strong&gt;not&lt;/strong&gt; be capitalized. If you begin a method name with an uppercase letter, Ruby might think that it is a constant. The convention is to use underscores to separate words if your method name has more than one word.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Define a Method
&lt;/h2&gt;

&lt;p&gt;Methods should be defined before calling them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A methods definition starts with the 'def' keyword followed by the methods name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Method parameters are specified between parentheses after the methods name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The methods definition ends with the 'end' keyword on the last line similar to a closing bracket for a function.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax example:&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;def method_name(argument)
  puts argument 
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Naming Methods
&lt;/h2&gt;

&lt;p&gt;Method names may end with a "!"(bang operator), a "?" or "=".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The bang methods(! at the end of method name) are called and executed just like any other method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Methods that end with a question mark by convention return a boolean. They may not always return just true or false, but usually they return an object which in ruby is a truthy value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Methods that end with an equals sign indicate an assignment method. For assignment methods the return value is ignored, the arguments are returned instead.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Arguments
&lt;/h2&gt;

&lt;p&gt;A Ruby method can accept arguments.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The list of arguments follows the method name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The parentheses around the arguments are optional.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multiple arguments are separated by a comma.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The arguments are positional.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax example:&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;def multiply_values(num1, num2)
  return num1 * num2 
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Default Values
&lt;/h2&gt;

&lt;p&gt;You can set default values of arguments. The default value does not need to be listed first, but arguments with defaults must be listed together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax example:&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;def multiply_values(num1, num2 = 6)
  return num1 * num2 
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>programming</category>
      <category>ruby</category>
    </item>
    <item>
      <title>useState Hook</title>
      <dc:creator>grant-ours</dc:creator>
      <pubDate>Fri, 26 Aug 2022 20:05:44 +0000</pubDate>
      <link>https://forem.com/grantours/usestate-hook-ak7</link>
      <guid>https://forem.com/grantours/usestate-hook-ak7</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yoc5IGgo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wql0h3vd1syffbzwplsr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yoc5IGgo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wql0h3vd1syffbzwplsr.png" alt="React Hooks useState" width="880" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Are Hooks?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hooks are new! It was a feature introduced in React 16.8. It allows you to use state and other React features without writing a class. Hooks are functions which "hook into" React features. It does not work inside classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When Should I &lt;code&gt;useState&lt;/code&gt; vs Props?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Choosing whether to use state vs props comes down to "who owns this data?". If data is managed by one component, but another component needs access to that data, you'd pass the data from the one component to the other component via &lt;em&gt;props&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If a component manages the data itself, it should use &lt;em&gt;state&lt;/em&gt; to manage it.&lt;/p&gt;

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

&lt;p&gt;I'm going to show how to use state in a simple cookie clicker app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Declaring State&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';

function Example() {
  // Declare a new state variable, which we'll call "cookies"
  const [cookies, setCookies] = useState(0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What Does Declaring State Do?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It declares a “state variable” and a callback function. Our variable is called &lt;code&gt;cookies&lt;/code&gt; but you could call it anything. The &lt;code&gt;setCookies&lt;/code&gt; function will set the new value of &lt;code&gt;cookies&lt;/code&gt;. This is a way to “preserve” some values between the function calls. Normally, variables “disappear” when the function exits but state variables are preserved by React.  We pass 0 into &lt;code&gt;useState&lt;/code&gt; to set our "initial state".  Meaning our cookie clicker count will start at 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Display Your Current State&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we want to display the current amount of cookies that we have we can use &lt;code&gt;cookies&lt;/code&gt; directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;You have {cookies} cookies!&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to Update State&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;  &amp;lt;button onClick={() =&amp;gt; setCookies(cookies + 1)}&amp;gt;
    Click for Cookie!
  &amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Thats it!
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 1:  import React, { useState } from 'react';
 2:
 3:  function Example() {
 4:    const [cookies, setCookies] = useState(0);
 5:
 6:    return (
 7:      &amp;lt;div&amp;gt;
 8:        &amp;lt;p&amp;gt;You have {cookies} cookies!&amp;lt;/p&amp;gt;
 9:        &amp;lt;button onClick={() =&amp;gt; setCookies(cookies + 1)}&amp;gt;
10:         Click for Cookie!
11:        &amp;lt;/button&amp;gt;
12:      &amp;lt;/div&amp;gt;
13:    );
14:  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We import the &lt;code&gt;useState&lt;/code&gt; hook from react.  This is how we keep track of state in our function.&lt;/li&gt;
&lt;li&gt;We declare state by calling our &lt;code&gt;useState&lt;/code&gt; hook, assign our state variable and callback function a name, and initialize state by passing a 0 indicating that we start with no cookies.&lt;/li&gt;
&lt;li&gt;Then, when a user clicks the button, we call setCookies with a new value. React will then re-render the Example component, passing the new cookies value to it.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Taking a Look at Conditional Statements</title>
      <dc:creator>grant-ours</dc:creator>
      <pubDate>Wed, 03 Aug 2022 23:56:00 +0000</pubDate>
      <link>https://forem.com/grantours/taking-a-look-at-conditional-statements-1ajb</link>
      <guid>https://forem.com/grantours/taking-a-look-at-conditional-statements-1ajb</guid>
      <description>&lt;h2&gt;
  
  
  &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else&lt;/code&gt;, and &lt;code&gt;else if&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;When you're writing code and you want to perform different actions based on different decisions.  This is where &lt;code&gt;if&lt;/code&gt; statements come in handy!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;if&lt;/code&gt; Statements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use an &lt;code&gt;if&lt;/code&gt; statement to run a specified block of code &lt;em&gt;if&lt;/em&gt; a certain condition is true.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example Structure of an &lt;code&gt;if&lt;/code&gt; statement&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;if (condition) {
   // insert code here to be executed if the condition is true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;else&lt;/code&gt; Statements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use an &lt;code&gt;else&lt;/code&gt; statement to run a specified block of code if the condition is false.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example Structure of an &lt;code&gt;else&lt;/code&gt; statement&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;if (condition) {
   // insert code here to be executed if the condition is true
}else{
   // insert code here to be executed if the condition is false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;else if&lt;/code&gt; Statements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use an &lt;code&gt;else if&lt;/code&gt; statement to add a second condition if the first condition is false.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example Structure of an &lt;code&gt;else&lt;/code&gt; statement&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;if (1st condition) {
   // insert code here to be executed if the condition is true
} else if (2nd condition) {
   // insert code here to be executed if the first condition is false and the second condition is true
} else {
   // insert code here to be executed if the first condition is false and the second condition is false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Pro Tip: Use console.log through out the process to figure out what is happening in your program and where!&lt;/em&gt;&lt;/p&gt;

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