Notice: These are personal notes for a course at my university on Web Development. I do not own any of this intellectual property, this is only used for personal education.

Table of Contents:

Lecture 7

Web Framekwork Pipeline:

The CRUD Pipeline:

Generating a Model

Generators for Migration

Commonly Used Data Types for models

Arel : Complex Query Methods for models

Common Arels (Relational Algebra)

User.where(name: 'Patrick')
#=> sends out query for all users names Patrick
User.find_by(name: 'Patrick')
#=> sends out query for ONE user named Patrick
User.limit(2)
#=> returns first two users, the order is ascending by id by default- first to last
User.order(id :desc)
#=> returns all users last to first

Validations: “Check before you commit!

Simple Validations

#=> simple validations inside Post model file
class Post < ActiveRecord:Base
    validates :name, prescence: true
    validates :name, uniqueness: true
    validates :content, lenth: {minimum: 5}
end

Errors Method

<% if @user.errors.any?%>
    <div id= "error_explanation">
      <h2>
        <%=@user.errors.count%> errors prohibited this from...
      </h2>
      <ul>
        <& @user.errors.full_messages.each do |msg|%>
            <li><%= msg &></li>
        <% end %>
      </ul>
<% end %>

Defining Custom Validations

Association: Lvl 1- One to Many

Implementation in Database

Dual Implementation in Ruby/Rails

@post = Post.find(1) 
#=>get post with id 1

@new_comment = @post.comments.build 
#=>equiv to post.new?
@new_comment.body = 'Comment for post 1'
@new_comment.save 

#=>alternative comment creation:
@new_comment = @post.comments.create({body: 'Comment for post 1'})

#=>find all comments belonging to a post:
@post = Post.find(1)
@comments = @post.comments

#=>find the post that a comment belongs to:
@comment = Comment.find(20)
@post = @comment.post

# recall that :
@post.comment.post == @post

Association Lvl2: Many to Many

Wrong Implementation in Database:

Correct Implementation in Database

Implementation of Many to Many in Ruby/Rails

Forms: Making POST Requests

<%= form_with url: '/login' do |form| &>
    <%= form.label :email %>
    <%= form.text_field :email %>
    <%= form.submit%>
<& end &> 
<%= form_with model: @user do |form| &>
    <%= form.label :email %>
    <%= form.text_field :email %>
    <%= form.submit%>
<& end &> 

Designing Forms: Log In Form

{
    email: '[email_field_value]'
    password: '[password_field_value]'
}

TODO: FINISH LEC 7


Lecture 8

Scaffold Generator:

#=> to generate:
rails g scaffold Post title:string content:text
#=> to destroy:
rails destroy scaffold Post

Scaffold Generator Demo

Arel Demo

Validations Demo

Class User < ApplicationController
  validates: first_name, :last_name, :presence: true
  validates :email, presence: true, uniqueness: true, length: {minimum: 5}
  validate :first_name_capitalized, :last_name_capitalized

#=> custom validations:
  private

  def first_name_capitalized
    errors.add(:first_name, "must be capitalized") if first_name && first_name.capitalize!
    #=> first_name is boolean as to whether its there or blank
    #=> first_name.capitalize is boolean true if capitalized
  end

  def last_name_capitalized
    errors.add(:last_name, "must be capitalized") if last_name && last_name.capitalize!
  end
end 

Associations Demo

def create
  @post = Post.find(...)
  @comment  = Comment.new(...)
end

Lecture 9