<?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: Mary Webby</title>
    <description>The latest articles on Forem by Mary Webby (@marywebby).</description>
    <link>https://forem.com/marywebby</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%2F1183709%2F032b3648-229a-4185-805b-42149851c176.png</url>
      <title>Forem: Mary Webby</title>
      <link>https://forem.com/marywebby</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/marywebby"/>
    <language>en</language>
    <item>
      <title>Photogram Authorization</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Mon, 11 Mar 2024 20:11:05 +0000</pubDate>
      <link>https://forem.com/marywebby/photogram-authorization-1942</link>
      <guid>https://forem.com/marywebby/photogram-authorization-1942</guid>
      <description>&lt;p&gt;➔ Notes &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;first begin with checking for traceable and editable routes that our users could use to change info that isn't theirs. if found, we will then need to go into our &lt;code&gt;routes.rb&lt;/code&gt; and add in &lt;code&gt;except&lt;/code&gt; and &lt;code&gt;only&lt;/code&gt; parameters to our &lt;code&gt;resources :follow_requests&lt;/code&gt; controller to not let other users be able to change who gets to follow and unfollow who. so after for photogram it would look something like this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resources :comments
resources :follow_requests, except: [:index, :show, :new, :edit]
resources :likes, only: [:create, :destroy]
resources :photos, except: [:index]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;next we can move onto the controller with &lt;code&gt;before_action&lt;/code&gt; in private, this action will allow us to add some blockers to specific routes so users can't just add and delete whichever photo they want to.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PhotosController &amp;lt; ApplicationController
  before_action :set_photo, only: %i[ show edit update destroy ]
  before_action :ensure_current_user_is_owner, only: [:destroy, :update, :edit]
  # ...
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;def ensure_current_user_is_owner
  if current_user != @photo.owner
   redirect_back fallback_location: root_url, alert: "You're not authorized for that."
end 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;To reiterate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First we ask what routes we actually want and filter them from the routes.rb with except or only. For the remaining routes, we ask who is allowed to do what on each route.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;ul&gt;
&lt;li&gt;we use conditionals for keeping edit and delete buttons cause users shouldn’t see the edit or delete links. For instance, we would put a conditional within the photo.html.erb in the &lt;code&gt;app/views/photos/_photo.html.erb&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      &amp;lt;% if current_user == photo.owner %&amp;gt;
        &amp;lt;%= link_to edit_photo_path(photo), class: "btn btn-link btn-sm text-muted" do %&amp;gt;
          &amp;lt;i class="fas fa-edit fa-fw"&amp;gt;&amp;lt;/i&amp;gt;
        &amp;lt;% end %&amp;gt;

        &amp;lt;%= link_to photo, method: :delete, class: "btn btn-link btn-sm text-muted" do %&amp;gt;
          &amp;lt;i class="fas fa-trash fa-fw"&amp;gt;&amp;lt;/i&amp;gt;
        &amp;lt;% end %&amp;gt;
      &amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We are checking that the current_user is the photo.owner in the view template, and only rendering the Font Awesome links if they are.&lt;br&gt;
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;hiding if users are private would require another conditional in the &lt;code&gt;app/views/users/show.html.erb&lt;/code&gt; file, this fix to the code would allow us to not see users if they are private, but to see them if we follow them
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;% if current_user == @user || !@user.private? || current_user.leaders.include?(@user) %&amp;gt;
  &amp;lt;div class="row mb-2"&amp;gt;
    &amp;lt;div class="col-md-6 offset-md-3"&amp;gt;
      &amp;lt;%= render "users/profile_nav", user: @user %&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;% @user.own_photos.each do |photo| %&amp;gt;
    &amp;lt;div class="row mb-4"&amp;gt;
      &amp;lt;div class="col-md-6 offset-md-3"&amp;gt;
        &amp;lt;%= render "photos/photo", photo: photo %&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;% end %&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;@user.private?&lt;/code&gt; is going to be true or false. The preceding ! asks “if not”, flipping the true or false. In other words saying, “if the user is not private then render the page”.&lt;br&gt;
&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;now we have to make it so users are only able to comment on photos in our app if they are following the user or the user is public, so in the actions of the &lt;code&gt;app/controllers/comments_controller.rb&lt;/code&gt;. we may have hid the route and/or content, but we also want to bounce them from actions they aren't allowed in.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CommentsController &amp;lt; ApplicationController
  before_action :set_comment, only: %i[ show edit update destroy ]
  before_action :is_an_authorized_user, only: [:destroy, :create]
  # ...
    def is_an_authorized_user
      if current_user == @comment.owner || !@comment.owner.private? || current_user.leaders.include?(@comment.owner)
        redirect_back fallback_location: root_url, alert: "Not authorized"
      end
    end
  # ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That copy-paste of the conditional won’t work, because we don’t have &lt;code&gt;@user&lt;/code&gt; here, we have &lt;code&gt;@comment&lt;/code&gt;. We need to get from &lt;code&gt;@comment&lt;/code&gt; to the owner of it, turns out we’ll need to add another association accessor first. so, we need to go into the comment model, and make sure we are getting the user of the photo through owner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Comment &amp;lt; ApplicationRecord
  belongs_to :author, class_name: "User", counter_cache: true
  belongs_to :photo, counter_cache: true
  has_one :owner, through: :photo

  validates :body, presence: true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could test that, but we would unfortunately end up getting a &lt;code&gt;ni&lt;/code&gt;l returned for &lt;code&gt;@comment&lt;/code&gt; when we try to create or destroy. Why? Because the &lt;code&gt;before_action :set_comment&lt;/code&gt; is not being run before our new &lt;code&gt;is_authorized_user&lt;/code&gt; method (the new method is not in the &lt;code&gt;only&lt;/code&gt; list for &lt;code&gt;:set_comment&lt;/code&gt;). Actually if we tried debugging this, we would realize a flaw in our logib up until this point, we dont want to be looking at the owner of the comment, we want to be looking at the owner of the photo of which we are commenting on, so we instead would be changing our code from before, to 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 CommentsController &amp;lt; ApplicationController
  before_action :set_comment, only: %i[ show edit update destroy ]
  before_action :is_an_authorized_user, only: [:destroy, :create]
  # ...
    def is_an_authorized_user
      @photo = Photo.find(params.fetch(:comment).fetch(:photo_id))
      if current_user != @photo.owner &amp;amp;&amp;amp; @photo.owner.private? &amp;amp;&amp;amp; !current_user.leaders.include?(@photo.owner)
        redirect_back fallback_location: root_url, alert: "Not authorized"
      end
    end
  # ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;first, we will be needing to get the current photo from the &lt;code&gt;params&lt;/code&gt; hash (from &lt;code&gt;comment[photo_id]&lt;/code&gt;), then we need to change the logic in our conditional (switching the location of the &lt;code&gt;!&lt;/code&gt; “if not” modifiers, and changing &lt;code&gt;||&lt;/code&gt; to &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) to get what we want: Users can only comment of public photos, or photos of their leaders.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ruby</category>
      <category>rails</category>
      <category>authorization</category>
    </item>
    <item>
      <title>Photogram Industrial Notes</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Fri, 08 Mar 2024 21:21:34 +0000</pubDate>
      <link>https://forem.com/marywebby/photogram-industrial-notes-4o57</link>
      <guid>https://forem.com/marywebby/photogram-industrial-notes-4o57</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;notes about scaffolds for CRUD &amp;amp; generating a model&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The question you have to answer now is: for each of these tables, do you want to generate a scaffold or do you just want to generate a model? How do we figure that out?&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If I will need routes and controller/actions for users to be able to CRUD records in the table, then I probably want to generate &lt;code&gt;scaffold&lt;/code&gt;. (At least some of the generated routes/actions/views will go unused. I need to remember to go back and at least disable the routes, and eventually delete the entire RCAVs, at some point; or I risk introducing security holes.)&lt;/p&gt;

&lt;p&gt;If the model will only be used on the backend, e.g. by other models, then I probably want to generate &lt;code&gt;model&lt;/code&gt;. For example, a &lt;code&gt;Month&lt;/code&gt; model where I will create all twelve records once in &lt;code&gt;db/seeds.rb&lt;/code&gt; does not require routes, &lt;code&gt;MonthsController&lt;/code&gt;, &lt;code&gt;app/views/months/&lt;/code&gt;, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;foreign keys in migrates &amp;amp; models&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;any times you are specifying the &lt;code&gt;forgein_key&lt;/code&gt; within a migrate file, you need to also make sure to go and change it in the model as well. so here is our migrate file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CreateComments &amp;lt; ActiveRecord::Migration[7.0]
  def change
    create_table :comments do |t|
      t.references :author, null: false, foreign_key: { to_table: :users }
      t.references :photo, null: false, foreign_key: true
      t.text :body

      t.timestamps
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;and we are specifically looking at the &lt;code&gt;foreign_key: { to_table: :users }&lt;/code&gt; part of this. and you can see we specified that the users table is going to be what the author is referring to. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;so now in our models, we can go into &lt;code&gt;comment.rb&lt;/code&gt;, and in here we can add in the specific classname for the author&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;class Comment &amp;lt; ApplicationRecord
  belongs_to :author, class_name: "User"
  belongs_to :photo
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Raghu: &lt;em&gt;"So basically for every &lt;code&gt;foreign_key&lt;/code&gt;, there should be a &lt;code&gt;belongs_to&lt;/code&gt;. For every &lt;code&gt;belongs_to&lt;/code&gt;, there should be a &lt;code&gt;has_many&lt;/code&gt;."&lt;/em&gt;&amp;lt;&lt;/p&gt;

&lt;p&gt;rails db:drop will delete your entire database, rails db:rollback will undo your last migration&amp;lt;&lt;/p&gt;

&lt;p&gt;will need to keep all the migration files to see all the history of changes and  &lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>Helper Methods</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Fri, 01 Mar 2024 16:12:35 +0000</pubDate>
      <link>https://forem.com/marywebby/helper-methods-acb</link>
      <guid>https://forem.com/marywebby/helper-methods-acb</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;➜ Cleaning up &lt;code&gt;config/routes.rb&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post("/movies", { :controller =&amp;gt; "movies", :action =&amp;gt; "create" })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;anywhere you see the old &lt;code&gt;Hash&lt;/code&gt; syntax (&lt;code&gt;:symbol&lt;/code&gt; keys with hash rockets &lt;code&gt;=&amp;gt;&lt;/code&gt;): switch to the new &lt;code&gt;Hash&lt;/code&gt; syntax. Which would be &lt;code&gt;controller: "movies", action: "create"&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;next, it is always good to see how many methods are happening on this line, Raghu's convention is that is there no other methods happening, you can leave out the parentheses. so our output would look something like this &lt;code&gt;get "/movies", controller: "movies", action: "create"&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we can even go further to just take out the controller and action since it is the only method we are placing into the route. remember, the &lt;code&gt;#&lt;/code&gt; symbol is used to indicate an &lt;code&gt;Object#method&lt;/code&gt;, which, in this example, is &lt;code&gt;MoviesController#create&lt;/code&gt;. which would finally leave us with something like this&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;get "/movies" =&amp;gt; "movies#create"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;for special circumstances like when you are wanting to display the root page of the website, rails has special syntax that allows you to omit the route being displayed, so instead of &lt;code&gt;# get("/", { :controller =&amp;gt; "movies", :action =&amp;gt; "index" })&lt;/code&gt;, you would write it as this &lt;code&gt;root "movies#index"&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;➜ Route helper methods&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;at the bash prompt, run &lt;code&gt;rails routes&lt;/code&gt;. the display should look like something similar to this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prefix Verb   URI Pattern                                Controller#Action
            GET    /                                         movies#index
    movies POST   /movies(.:format)                          movies#create
movies_new GET    /movies/new(.:format)                      movies#new
            GET    /movies(.:format)                         movies#index
            GET    /movies/:id(.:format)                     movies#show
            PATCH  /movies/:id(.:format)                     movies#update
            GET    /movies/:id/edit(.:format)                movies#edit
            DELETE /movies/:id(.:format)                     movies#destroy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Prefix&lt;/code&gt; means that Rails has defined a method that we can call anywhere in our view templates, controller actions, etc., that will return the path as a string. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;to test out what exactly this code is doing, move over to the &lt;code&gt;index.html.erb&lt;/code&gt; page, type in &lt;code&gt;&amp;lt;%= movies_path %&amp;gt;&lt;/code&gt;, and see what the page renders. you can see that it is displaying the string &lt;code&gt;"/movies"&lt;/code&gt;. if we were to try others, we can see that &lt;code&gt;&amp;lt;%= movies_new_path %&amp;gt;&lt;/code&gt; refresh the change, and &lt;code&gt;/movies&lt;/code&gt; renders &lt;code&gt;"/movies/new"&lt;/code&gt;. Following this pattern, &lt;code&gt;&amp;lt;%= root_path %&amp;gt;&lt;/code&gt; just renders &lt;code&gt;"/"&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;movies&lt;/code&gt; and &lt;code&gt;movies_new&lt;/code&gt; are static routes, without dynamic ID numbers in the path, so Rails just puts the segments together and gives you the method. But, for all the rest of the routes that do contain &lt;code&gt;:id&lt;/code&gt;, there are no methods defined yet. We get to define those methods ourselves!&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;➜ Creating our own route helper methods&lt;/strong&gt;
&lt;/h2&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get "/movies/:id" =&amp;gt; "movies#show", get "/movies/:id" =&amp;gt; "movies#show", as: :details
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;say for the one above, &lt;code&gt;"/movies/:id"&lt;/code&gt; has &lt;code&gt;:id&lt;/code&gt; within the route, so we have to properly define it after the route, just like we are doing &lt;strong&gt;above&lt;/strong&gt;, the &lt;code&gt;as: :details&lt;/code&gt; part of the route is us defining a specific method, we can then transition back into the &lt;code&gt;index.html.erb&lt;/code&gt; file and define our route like so &lt;code&gt;&amp;lt;%= details_path %&amp;gt;&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if we see what this provides us with it will be an error &lt;code&gt;No route matches {:action=&amp;gt;"show", :controller=&amp;gt;"movies"}, missing required keys: {:id}&lt;/code&gt;. this is because it is a dynamic route, so rails cant produce a route unless you tell it what to put in the path! &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;say if we were to put &lt;code&gt;(42)&lt;/code&gt; within this ruby syntax helper method, it would be able to display the route with the &lt;code&gt;:id&lt;/code&gt; being &lt;code&gt;(42)&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;you can also change the embedded ruby method to &lt;code&gt;details_url(42)&lt;/code&gt;, then you will be able to see the full url of the app, including the path, this allows you to see fully qualified url's. this means you dont need to hardcode anything because it will be able to full run on any persons own personal web browser. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;now we are going to be placing them into the normal conventions that every ruby developers use. such as&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;get "/movies/new" =&amp;gt; "movies#new", as: :new_movie
get "/movies/:id/edit" =&amp;gt; "movies#edit", as: :edit_movie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;we however dont need to define the helper methods for delete or patch
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;patch "/movies/:id" =&amp;gt; "movies#update"
delete "/movies/:id" =&amp;gt; "movies#destroy"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;➜ implementing helper methods&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Now we are going to be taking those helper methods and placing them into the rest of our code, so these are going to work the same way as routes, where instead of having the routes path and then placing the variable into it with ruby &lt;code&gt;&amp;lt;%= a_movie.id %&amp;gt;&lt;/code&gt; like this, we will change it to something like below...
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action="&amp;lt;%= movie_path(@the_movie.id) %&amp;gt;" method="post" data-turbo="false"&amp;gt;
  &amp;lt;input name="authenticity_token" value="&amp;lt;%= form_authenticity_token %&amp;gt;" type="hidden"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;another point, since we are taking the specific movie id for the route, we can use ruby convention and just always assume that that is what it will naturally grab specifically unless we say otherwise &lt;code&gt;action="&amp;lt;%= movie_path(@the_movie) %&amp;gt;"&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;say we are implementing them into our controllers for redirection, for this piece of code for example...&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;if @the_movie.valid?
      @the_movie.save
      redirect_to("/movies", { :notice =&amp;gt; "Movie created successfully." })
    else
      render template: "movies/new"
    end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;we can instead change &lt;code&gt;"/movies"&lt;/code&gt; to &lt;code&gt;movies_url&lt;/code&gt;, since we want to specifically route them to a url and not use the path name &lt;code&gt;/movies&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;➜ Creating forms with helper methods&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= form_with(url: movies_path) do %&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;!--    &amp;lt;label for="title_box"&amp;gt;
      Title
    &amp;lt;/label&amp;gt;--&amp;gt;
    &amp;lt;%= label_tag :title_box, "Title" %&amp;gt;
    &amp;lt;!--&amp;lt;input type="text" id="title_box" name="query_title" value="&amp;lt;%#= @the_movie.title %&amp;gt;"&amp;gt;--&amp;gt;
    &amp;lt;%= text_field_tag :query_title, @the_movie.title, { id: "title_box" } %&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;div&amp;gt;
    &amp;lt;!--&amp;lt;label for="description_box"&amp;gt;
      Description
    &amp;lt;/label&amp;gt;--&amp;gt;
    &amp;lt;%= label_tag :description_box, "Description" %&amp;gt;
&amp;lt;!--    &amp;lt;textarea id="description_box" name="query_description" rows="3"&amp;gt;&amp;lt;%#= @the_movie.description %&amp;gt;&amp;lt;/textarea&amp;gt;\ --&amp;gt;
    &amp;lt;%= text_area_tag :query_description, @the_movie.description, id: "description_box", rows: 3 %&amp;gt;
  &amp;lt;/div&amp;gt;    

 &amp;lt;!-- &amp;lt;button&amp;gt;
    Create Movie
  &amp;lt;/button&amp;gt;--&amp;gt;
    &amp;lt;%= button_tag "Button"%&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;here you can see the before and after of placing in tags to our forms, and below is a much more organized way to look at it
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= form_with(url: movies_path) do %&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;%= label_tag :title_box, "Title" %&amp;gt;
    &amp;lt;%= text_field_tag :query_title, @the_movie.title, { id: "title_box" } %&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;div&amp;gt;
    &amp;lt;%= label_tag :description_box, "Description" %&amp;gt;
    &amp;lt;%= text_area_tag :query_description, @the_movie.description, id: "description_box", rows: 3 %&amp;gt;
  &amp;lt;/div&amp;gt;    

    &amp;lt;%= button_tag "Button"%&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;➜ &lt;code&gt;find_by&lt;/code&gt; method&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;what we have been practicing before has led us to this moment where we will be totally deducing our 3 steps to get an &lt;code&gt;ActiveRecord&lt;/code&gt; instance (a single row of a table, compared to an &lt;code&gt;ActiveRecord:Relation&lt;/code&gt; (which is many rows of a table. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in the beginning of learning ruby, we had something very similar to this setup in our code...&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;def show
    the_id = params.fetch(:id) # FIRST we get the ID

    matching_movies = Movie.where(id: the_id) # THEN we get the set of matching rows (ActiveRecord:Relation)

    @the_movie = matching_movies.first # FINALLY we get one instance of ActiveRecord, or one row
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;now we are presented with something very similar to this, essentially reducing whatever we started with to a more simplified version, such as this...
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def show
    @the_movie = Movie.where(id: params.fetch(:id)).first
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;now, finally we are going to be stripping that even more to a better more conventional version of this piece of code, something using the &lt;code&gt;.find_by&lt;/code&gt; method. it is essentially the same thing as where, except that is automatically returns the &lt;code&gt;.first&lt;/code&gt; record given in a collection without having to explicitly say that.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  def show
    @the_movie = Movie.find_by(id: params.fetch(:id)) 
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or &lt;em&gt;shorter&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;def show
    @the_movie = Movie.find(params.fetch(:id))
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;important to note here that the &lt;code&gt;.find()&lt;/code&gt; method only takes an integer argument and is specifically for finding ID's in the ID column table&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;now we can accurately and effectively search for what we need without using lines and lines of code as we were doing before. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;➜ Creating a checkbox form&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  getting an array with checkbox
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form&amp;gt;
  &amp;lt;label&amp;gt;Red&amp;lt;/label&amp;gt;
   &amp;lt;input type="checkbox" name="zebra[]" value="red"&amp;gt; 

  &amp;lt;label&amp;gt;Blue&amp;lt;/label&amp;gt;
  &amp;lt;input type="checkbox" name="zebra[]" value="blue"&amp;gt; 
&amp;lt;/form&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;output looks something like this&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;zebra["red", "blue"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  getting an array with hashes with checkbox
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form&amp;gt;
  &amp;lt;label&amp;gt;Red&amp;lt;/label&amp;gt;
   &amp;lt;input type="checkbox" name="zebra[giraffe]" value="red"&amp;gt; 

  &amp;lt;label&amp;gt;Blue&amp;lt;/label&amp;gt;
  &amp;lt;input type="checkbox" name="zebra[elephant]" value="blue"&amp;gt; 
&amp;lt;/form&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;output looks something like this&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;zebra["giraffe" =&amp;gt; "red", "elephant" =&amp;gt; "blue"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;➜ Mass assignment with forms&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= form_with(model: @movie, data: { turbo: false }) do |form| %&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;%= form.label :title %&amp;gt;

    &amp;lt;%= form.text_field :title %&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;div&amp;gt;
    &amp;lt;%= form.label :description %&amp;gt;

    &amp;lt;%= form.text_area :description, { rows: 3 } %&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;%= form.button %&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;➜ Adding method&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This movie_params is a good tool to understand and takes away the need to write in a new table over and over again when making it cause you would just add it in one place and then it could be added everywhere.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def movie_params
    params.require(:movie).permit(:title, :description, :image_url, :director_id)
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;➜ partial view templates&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You can only add a partial to a big, not a big to a big or a big to a partial. this helps up organize our code into more folders and shorter lines so we are not overwhelmed by how many lines could be for each page.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= render "shared/flash_messages", locals: { person: "Alice" }%&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faghac7pbv3y1cec8y30g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faghac7pbv3y1cec8y30g.png" alt="Image description" width="504" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;could be good for explaining name for each user in the top corner &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in the file youre displaying&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;&amp;lt;%= render partial: "movies/form", locals: { foo: @the_movie} %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;in the partial
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;% foo.errors.full_messages.each do |message| %&amp;gt;
  &amp;lt;p style="color: red;"&amp;gt;&amp;lt;%= message %&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Bulletin Board User Auth</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Wed, 21 Feb 2024 15:44:17 +0000</pubDate>
      <link>https://forem.com/marywebby/bulletin-board-user-auth-l6i</link>
      <guid>https://forem.com/marywebby/bulletin-board-user-auth-l6i</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;setting up the rails generate devise:install&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate devise:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;paste into terminal after running &lt;code&gt;rake sample_data&lt;/code&gt;, and &lt;code&gt;bin/dev&lt;/code&gt;. this will generate the following
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Depending on your application's configuration some manual setup may be required:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:

       config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

     In production, :host should be set to the actual host of your application.

     * Required for all applications. *

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root to: "home#index"

     * Not required for API-only Applications *

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

       &amp;lt;p class="notice"&amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;
       &amp;lt;p class="alert"&amp;gt;&amp;lt;%= alert %&amp;gt;&amp;lt;/p&amp;gt;

     * Not required for API-only Applications *

  4. You can copy Devise views (for customization) to your app by running:

       rails g devise:views

     * Not required *
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We complete sets 1 through 3 for this project, but you are also allowed to complete step 4, we did not go into that. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;why are we doing this&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Before, we were creating our databases with our models and columns in psql, which is good for initially setting up our db, but say we want to add things further down the line with other columns we need to add. Here, we are using db migrate and creating migration files to be able to add more to our db.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So initially, we need to create the migration file, which we can do by running &lt;code&gt;rails generate migration AddUserIdToBoards user_id:integer&lt;/code&gt; for example. This will create the migration file for us to use, which we can then run &lt;code&gt;rake db:migrate&lt;/code&gt; to then allow our database to officially implement those changes. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if we were to accidentally run &lt;code&gt;rake db:migrate&lt;/code&gt;, we would run into the issue of having the database already initialized, and would have to start over again, which is what i was stating previously in the section above. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Bulletin Board 1</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Tue, 20 Feb 2024 17:12:36 +0000</pubDate>
      <link>https://forem.com/marywebby/bulletin-board-1-5f6</link>
      <guid>https://forem.com/marywebby/bulletin-board-1-5f6</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;setting up db with rails console&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate draft:resource &amp;lt;MODEL_NAME_SINGULAR&amp;gt; &amp;lt;COLUMN_1_NAME&amp;gt;:&amp;lt;COLUMN_1_DATATYPE&amp;gt; &amp;lt;COLUMN_2_NAME&amp;gt;:&amp;lt;COLUMN_2_DATATYPE&amp;gt; # etc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to generate a complete, database-backed web resource, use the code snippet above. It is actually exactly the same as creating and generating a model and a table, but instead model is replaced with &lt;code&gt;draft:resource&lt;/code&gt;. below is an example of using 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 draft:resource post title:string body:text expires_on:date board_id:integer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or...in our assignment here...&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 draft:resource board name:string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this action that we just did has essentially installed all of our standard CRUD action boilerplate for us (navigate to &lt;code&gt;/posts&lt;/code&gt;. if you would like to delete the action you just made, you can also &lt;code&gt;rails destroy draft:resource&lt;/code&gt;, &lt;code&gt;rake db:rollback&lt;/code&gt;, &lt;code&gt;rails destroy draft:resource post&lt;/code&gt; or go back to the last git commit. you can then correct your error and then regenerate. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;adding and removing columns&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To modify existing tables, the most common tools we use are &lt;code&gt;add_column&lt;/code&gt; and &lt;code&gt;remove_column&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;generate a new migration (not a whole model like above) at a bash prompt with, for example, use &lt;code&gt;rails g migration AddImageToPosts&lt;/code&gt;, or &lt;code&gt;rails g migration RemoveExpiresOnFromPosts&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;then, go into the new migration file and add instructions to make the &lt;code&gt;change&lt;/code&gt; you want within the change method (or the &lt;code&gt;up&lt;/code&gt; method, if that’s what you find inside instead of change)&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;def change
  add_column :posts, :image, :string
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;with ultimate last resort, use &lt;code&gt;rake db:drop&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;other notes&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;rspec rails framework &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;bundle exec rspec&lt;/code&gt;or &lt;code&gt;rspec&lt;/code&gt; to run rails debugging with ruby gem inside the GemFile&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Refactoring MSM GUI 2</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Mon, 19 Feb 2024 19:35:41 +0000</pubDate>
      <link>https://forem.com/marywebby/refactoring-msm-gui-2-3fah</link>
      <guid>https://forem.com/marywebby/refactoring-msm-gui-2-3fah</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Creating a standard instance method&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Standard practice for getting a specific piece of information from each database
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Character &amp;lt; ApplicationRecord

  def movie
    key = self.movie_id

    matching_set = Movie.where({ :id =&amp;gt; key })

    the_one = matching_set.at(0)

    return the_one
  end

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;meta methods&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;belongs_to&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;belongs_to( :name_that_we_want, :class_name =&amp;gt; "", :foreign_key =&amp;gt; "" )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so for example, if you wanted to change the code above, you would turn the prompt given above into this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;belongs_to( :movie, :class_name =&amp;gt; "Movie", :foreign_key =&amp;gt; "movie_id"  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;since you are typically name all of these the same convention, you can even go further and take out the classname.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;belongs_to(:movie, :foreign_key =&amp;gt; "movie_id" )

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

&lt;/div&gt;



&lt;p&gt;they decided to write this method if you omit the class name, they're going to guess that the class name matches the method name that you select, which is the case 99% of the time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;belongs_to(:movie)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;has_many&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;has_many( :name_that_we_want, :class_name =&amp;gt; "", :foreign_key =&amp;gt; "" )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;producing a lot from this specific method so we want to use it for when we would we would be getting information back that comes in many, say characters, or comments, or movies. stuff like that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;belongs_to( :name_that_we_want )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can also deduce it the same way like before if the class name matches the method name, then rails will assume that that is the name that you are going to use unless stated otherwise.  &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;example on how to use in rails console&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;type these into rials console to understand where we can add on methods to meta methods. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Character.joins(:movie).where("movies.year &amp;gt; 1994")&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Photogram GUI (graphical user interface)</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Mon, 19 Feb 2024 16:34:47 +0000</pubDate>
      <link>https://forem.com/marywebby/photogram-gui-graphical-user-interface-2nk5</link>
      <guid>https://forem.com/marywebby/photogram-gui-graphical-user-interface-2nk5</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Difference in &lt;code&gt;redirect_to&lt;/code&gt; in contrast to &lt;code&gt;render&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;render()&lt;/code&gt; is only very beneficial in terms of when you are using it to display one piece of information per page. this means that everything in your action of your controller is really all the information that you need for the page. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in contrast to &lt;code&gt;redirect_to()&lt;/code&gt;, this specific type of page rendering allows you to sort of &lt;code&gt;push&lt;/code&gt; all the information you acquired in within your action and allow it to be placed into another page, with other info from other actions. This helps in very dynamic and info packed pages where you want multiple different sections doing different things but don't want it to all be in one controller. for example, when you are editing and adding new photos to a users page, but also want to display and post comments on the photos. You would have those 3 different actions, all redirecting to the same page. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Understanding what &lt;code&gt;.where&lt;/code&gt; returns&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;when searching for a specific user within an action, you need to search within all the Users model, and then pick that id.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def show
    username = params.fetch("username")
    matching_users = User.where({ :username =&amp;gt; username })
    @user = matching_users.at(0)

    render({ :template =&amp;gt; "user_templates/user_details"})
  end 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the specific line to note here is &lt;code&gt;@user = matching_users.at(0)&lt;/code&gt;this is because we are returned a hash, and need to select the first position of for the specific user we want. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Data Integrity w/ Validations</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Wed, 14 Feb 2024 17:25:13 +0000</pubDate>
      <link>https://forem.com/marywebby/data-integrity-w-validations-4pg</link>
      <guid>https://forem.com/marywebby/data-integrity-w-validations-4pg</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Invalid Records History&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Reminder: to run the rails console, you need to run either &lt;code&gt;rails c&lt;/code&gt; or &lt;code&gt;rails console&lt;/code&gt; in the terminal to prompt, even though it is &lt;code&gt;pry&lt;/code&gt; you cannot just run &lt;code&gt;pry&lt;/code&gt; in the terminal to get it started, you will need to run the other one to make sure that all you models and tables are being queried correctly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As soon as you start serving data from external sources (ex. users, CSVs, or APIs) you have to start worrying about whether that data is &lt;em&gt;valid&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you do encounter &lt;em&gt;invalid&lt;/em&gt; records and they become apart of your database, you will have to begin writing your code defensively, utilizing &lt;code&gt;if/elsif/else/end&lt;/code&gt;, just to guard against the invalid data. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It’s best not to allow invalid data that doesn’t meet our criteria to enter our database in the first place; then, we don’t need to worry about writing lots of defensive conditionals downstream in our code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ActiveRecord provides the feature: &lt;strong&gt;validations&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Validations are a way that we can specify constraints on what we allow in each column in a model. It is crucial that we have the &lt;code&gt;.save&lt;/code&gt; method, this will allow us not to place records into the database we don't want to. Thus, returning to us &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Validations are a super-useful tool that ActiveRecord provides for maintaining the integrity of our data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How to Insert Validations in Models&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;question to address&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
let’s say we don’t want to allow any rows to enter our movies table with a blank &lt;code&gt;director_id&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;how to fix &amp;amp; add validations&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
to address this problem we want to move into the &lt;code&gt;Movie&lt;/code&gt; model insert a Validation inside of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Movie &amp;lt; ApplicationRecord
  validates(:director_id, presence: true)

  def director
  # ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first argument to validates is a Symbol; the name of the column we are declaring constraints for.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second argument to validates is a Hash.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The keys in the Hash are the constraints we are applying. There is a fixed list of constraints built-in to ActiveRecord that we can choose from. By far, the three most common are :presence, :uniqueness, and :numericality.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the value that we are adding to either &lt;code&gt;true&lt;/code&gt;, which would allow you wanting the simplest use of validation. On the other hand, you can also have a hash in there clarifying the configuration options, like whether you want want the &lt;code&gt;:numerically&lt;/code&gt; validation to have maximum or minimum &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Testing our New Validations with Tests&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;type these commands inot the &lt;code&gt;rails console&lt;/code&gt; termnial&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;m = Movie.new&lt;/code&gt; creating a new instance of a movie&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the return value should look something like this&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;[1] pry(main)&amp;gt; m = Movie.new
   (0.4ms)  SELECT sqlite_version(*)
=&amp;gt; #&amp;lt;Movie:0x00007f93ab387430
 id: nil,
 title: nil,
 year: nil,
 duration: nil,
 description: nil,
 image: nil,
 director_id: nil,
 created_at: nil,
 updated_at: nil&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;next, we will type &lt;code&gt;m.save&lt;/code&gt;, which if done correctly should return a &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;we can double check that its not in there by typing &lt;code&gt;m&lt;/code&gt; into the console again to see that there was no &lt;code&gt;created_at:&lt;/code&gt; or &lt;code&gt;updated_at:&lt;/code&gt; values inserted so we know that it was never created. &lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;The Errors Collection&lt;/strong&gt;
&lt;/h3&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we can even find out &lt;em&gt;why&lt;/em&gt; the object didn’t save. each ActiveRecord object has an attribute called errors, which contains an instance of ActiveModel::Errors — a collection of validation failures&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we can then type &lt;code&gt;m.errors&lt;/code&gt; into our terminal and ActiveModel::Errors will prompt an output like this&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;[4] pry(main)&amp;gt; m.errors
=&amp;gt; #&amp;lt;ActiveModel::Errors [#&amp;lt;ActiveModel::Error attribute=director_id, type=blank, options={}&amp;gt;]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;next we can try to get the message
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[5] pry(main)&amp;gt; m.errors.messages
=&amp;gt; {:director_id=&amp;gt;["can't be blank"]}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;there is also a very helpful method we can place onto the error which will return a list of arrays of detailed messages on how to fix our issue on why it is not saving.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[6] pry(main)&amp;gt; m.errors.full_messages
=&amp;gt; ["Director can't be blank"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;later on down the road we can actually take those error messages, say in like logging in to an account or making a password and actually provide the user the string from the error to realize their mistakes and fix the issue. this is a good alternative than creating a for loop and making it so you have a ton of code. you can really be utilizing the ActiveRecord::Errors to make it so you are personally doing less code and using more resources. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Helpful Methods for Validations&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;validates(:something_id, presence: true)&lt;/code&gt; checks to see if a a specific "something" in a table has the presents of the thing it needs before saving to the table&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;validates(:something, uniqueness: true)&lt;/code&gt; checks to see if no other rows have the same value in that column, very helpful for usernames and stuff where each value has to be unique to the specific id so there is no confusion. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Instance Method Review</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Tue, 13 Feb 2024 21:58:42 +0000</pubDate>
      <link>https://forem.com/marywebby/instance-method-review-26d0</link>
      <guid>https://forem.com/marywebby/instance-method-review-26d0</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Composing methods to build more powerful methods&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In ruby, we use the &lt;code&gt;def&lt;/code&gt; keyword within the &lt;code&gt;class&lt;/code&gt; definition to add new methods to a class
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person
  def say_hi
    return "Hello!"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;the &lt;code&gt;say_hi&lt;/code&gt; comes immediately after the &lt;code&gt;def&lt;/code&gt; keyword, Ruby knows we are defining an instance-level method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every method’s main job is &lt;em&gt;returning&lt;/em&gt; a value&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our programs are essentially just a long sequence of methods being called on the return values of previous methods, until we arrive at our desired output.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The self keyword&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person
  attr_accessor(:first_name)
  attr_accessor(:last_name)

  def full_name
    assembled_name = self.first_name + " " + self.last_name

    return assembled_name
  end

  def full_name_caps
    return self.full_name.upcase
  end
end

rb = Person.new
rb.first_name = "Raghu"
rb.last_name = "Betina"

pp rb.full_name_caps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;self as a special variable that holds whatever object is at the forefront of Ruby’s mind when it is evaluating a program. Whatever line of code it is reading, whatever object is it working on at the moment, that is what self contains.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;self&lt;/code&gt; represents the instance of the class that the method will be called upon in the future, formally, this object is known as the receiver of the message that is the method invocation. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Defining "association accessors"&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Whenever you see &lt;code&gt;Director#filmography&lt;/code&gt;, that is the &lt;code&gt;Director&lt;/code&gt; class, and there is a &lt;code&gt;filmography&lt;/code&gt; instance method within it. &lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Social Media Set up Tables</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Tue, 13 Feb 2024 16:51:34 +0000</pubDate>
      <link>https://forem.com/marywebby/social-media-set-up-tables-3ec0</link>
      <guid>https://forem.com/marywebby/social-media-set-up-tables-3ec0</guid>
      <description>&lt;p&gt;1.users &lt;br&gt;
-id&lt;br&gt;
-email&lt;br&gt;
-password&lt;br&gt;
-username&lt;br&gt;
-name&lt;br&gt;
-website_url&lt;br&gt;
-avatar_image_url&lt;br&gt;
-bio&lt;br&gt;
-private&lt;br&gt;
-liked_count&lt;/p&gt;

&lt;p&gt;2.follow_requests&lt;br&gt;
-id&lt;br&gt;
-created_at&lt;br&gt;
-updated_at&lt;br&gt;
-sender_id (users)&lt;br&gt;
-recipient_id (users)&lt;br&gt;
-status (accepted, rejected, pending) &lt;/p&gt;

&lt;p&gt;3.posts/photo&lt;br&gt;
-id &lt;br&gt;
-created_at&lt;br&gt;
-updated_at&lt;br&gt;
-user_id &lt;br&gt;
-image_url&lt;br&gt;
-caption&lt;br&gt;
-like_count(automatically incs or decs with CRUD)&lt;/p&gt;

&lt;p&gt;4.likes-join table (the like to the user)&lt;br&gt;&lt;br&gt;
-id&lt;br&gt;
-post_id &lt;br&gt;
-user_id &lt;/p&gt;

&lt;p&gt;5.comments - join table (the comment to the user) &lt;br&gt;
-id&lt;br&gt;
-created_at&lt;br&gt;
-updated_at&lt;br&gt;
-description&lt;br&gt;
-post_id &lt;br&gt;
-user_id &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Ruby Database Setup &amp; Common Commands &amp; CRUD</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Thu, 08 Feb 2024 18:12:17 +0000</pubDate>
      <link>https://forem.com/marywebby/ruby-database-setup-common-commands-crud-5g93</link>
      <guid>https://forem.com/marywebby/ruby-database-setup-common-commands-crud-5g93</guid>
      <description>&lt;p&gt;Using PSQL in the rails database. Explaining set up and then going into common methods for accessing data within the database.&lt;/p&gt;

&lt;p&gt;Note ➔ When editing and creating files within your app and outside of your terminal sql editor, you will need to start and restart the rails console to make sure the changes are incorporated. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;➔ Commands to start your db for rails&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;First, check out my other article on using SQL to create a table and the first object inside. &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/marywebby" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DQ6t4bPU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.dev.to/cdn-cgi/image/width%3D150%2Cheight%3D150%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto/https%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Fuser%252Fprofile_image%252F1183709%252F032b3648-229a-4185-805b-42149851c176.png" alt="marywebby"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/marywebby/notes-on-databases-and-sql-commands-for-reference-3k01" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Notes on Databases and SQL Commands for Reference&lt;/h2&gt;
      &lt;h3&gt;Mary Webby ・ Feb 7&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;
 &lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, you will need to change your &lt;code&gt;config/database.yml&lt;/code&gt; file to add your database you created. For example, &lt;code&gt;database: my_contact_book&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next you will be adding your class file to the models folder, so for example, &lt;code&gt;app/models/class.rb&lt;/code&gt;. In this folder will be, the base set up whats below. &lt;code&gt;&amp;lt; ActiveRecord::Base&lt;/code&gt; allows &lt;code&gt;Class&lt;/code&gt; to inherit all properties and methods in ActiveRecord. &lt;code&gt;class Class &amp;lt; ActiveRecord::Base&lt;/code&gt;\nextLine&lt;code&gt;end&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhm0mm9wvmoipspq20n2v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhm0mm9wvmoipspq20n2v.png" alt="Image description" width="620" height="172"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You are also able to &lt;code&gt;self.table_name = "contacts"&lt;/code&gt; and more properties to your class to manually enter here, instead of the command line. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You will now need to open the terminal and type in &lt;code&gt;irb&lt;/code&gt;, which will prompt the "interactive ruby", which is a tool to interactively execute Ruby expressions read from the standard input. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then you will &lt;code&gt;require "./app/models/class"&lt;/code&gt; to ensure you have the file you need. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Exit &lt;code&gt;irb&lt;/code&gt; by executing &lt;code&gt;exit&lt;/code&gt;, and type &lt;code&gt;rails c&lt;/code&gt; or &lt;code&gt;rails console&lt;/code&gt;, which will open the respective ruby PSQL. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;➔ Methods to act on your class within the &lt;code&gt;rails console&lt;/code&gt;.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;ActiveRecord Relations are very similar to Arrays. Any method that you can call on an Array, you can also call on a Relation; .at, .each, .sample, etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Class.count&lt;/code&gt; returns an integer &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Contact.all&lt;/code&gt; returns all the records in a table &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;x.at(0)&lt;/code&gt; x being and example for what to replace, and at(0) being where you are looking in the indexes. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;c = Contact.all.sample&lt;/code&gt; taking out a random contact and storing it in the variable c. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;c.id&lt;/code&gt;, &lt;code&gt;c.created_at&lt;/code&gt;, &lt;code&gt;c.first_name&lt;/code&gt;, &lt;code&gt;c.phone&lt;/code&gt;, now we can call on that specific variable we created and access its contents. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Contact.all.order({ :last_name =&amp;gt; :asc, :first_name =&amp;gt; :asc, :date_of_birth =&amp;gt; :desc })&lt;/code&gt; retrieve info and sorting it in a specific way. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Contact.order(:first_name).reverse_order&lt;/code&gt; same thing with this one. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Contact.where({ :last_name =&amp;gt; ["Betina", "Woods"] })&lt;/code&gt; giving specific to try and find a name. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Contact.where({ :first_name =&amp;gt; "Mickey" }).or(Contact.where({ :last_name =&amp;gt; "Betina" }))&lt;/code&gt; Similar ask to before but instead you are broadening the search. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;➔ CREATE READ UPDATE &amp;amp; DELETE&lt;/strong&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  UPDATE
&lt;/h4&gt;

&lt;p&gt;To update a row, first you need to locate it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c = Contact.where({ :id =&amp;gt; 2 }).first
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then assign whatever new values you want to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c.first_name = "Minerva"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then don’t forget to save the changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c.save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  DELETE
&lt;/h4&gt;

&lt;p&gt;To delete a row, first find it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c = Contact.where({ :id =&amp;gt; 2 }).first
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c.destroy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Intense.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;➔ Creating tasks to populate helpful sample data&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;code&gt;lib/tasks&lt;/code&gt;, and create file to create your task, such as &lt;code&gt;lib/tasks/i_am_lazy.rake&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Place your task into the file,&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3fc8xkrpa88zwaoz2c7f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3fc8xkrpa88zwaoz2c7f.png" alt="Image description" width="800" height="560"&gt;&lt;/a&gt; or any task you want to create. This one is creating a new contact for a contact book. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In our terminal we will call &lt;code&gt;rake sample_contacts&lt;/code&gt; and if you look at your database it should be populated! &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>ruby</category>
      <category>sql</category>
      <category>crud</category>
      <category>notes</category>
    </item>
    <item>
      <title>Notes on Databases and SQL Commands for Reference</title>
      <dc:creator>Mary Webby</dc:creator>
      <pubDate>Wed, 07 Feb 2024 22:08:40 +0000</pubDate>
      <link>https://forem.com/marywebby/notes-on-databases-and-sql-commands-for-reference-3k01</link>
      <guid>https://forem.com/marywebby/notes-on-databases-and-sql-commands-for-reference-3k01</guid>
      <description>&lt;ul&gt;
&lt;li&gt;Databases are a great tool to outsource the work of making a ruby file, CSV, or PStore&lt;/li&gt;
&lt;li&gt;Databases are seperate from apps themselves, so any language apps can use any databases (MySQL, PostgreSQL, SQLite, etc.)&lt;/li&gt;
&lt;li&gt;The interface to databases is text

&lt;ul&gt;
&lt;li&gt;Dont usually come with a GUI (graphical user interface)
Regional databses (which powers most apps) use a special 
language called SQL (stuctural query language)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Working directly with Postgres&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Great one to use cause it is already installed on codespaces in github.&lt;/li&gt;
&lt;li&gt;Commands to set up a table

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;psql&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;If this command went well, we should see postgres=#&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\list&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;displays all current databases on this computer&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CREATE DATABASE my-db&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;the name of the database should be snake_case&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\connet my-db&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;to enter databases&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\dt&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;lists all the tables in the db, this means “describe 
tables”. the realtion comes from the term “relational 
databases” a ‘relation’ is a set of multiple records. not 
related to “relationships”, in the context of one to many, 
or many to many, or one to one&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CREATE TABLE contacts (&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;id SERIAL PRIMARY KEY,&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;first_name TEXT,&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;last_name TEXT,&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;&lt;code&gt;date_of_birth DATE )&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;standard creation of a contact for use&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SELECT first_name, last_name FROM contacts&lt;/code&gt; 

&lt;ul&gt;
&lt;li&gt;use &lt;code&gt;*&lt;/code&gt; instead to see all&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;INSERT INTO contacts&lt;/code&gt;(all the values above)&lt;code&gt;VALUES ('mary', 'webby', '1999, 29, 12')&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;place single quotes around each intserted value.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

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