Hash in Ruby is key value pairs

:id 3
:status "Status"
:zombie "Jim"

The keys are called Symbols because of the colon in front.

hash = {
  id: 3,
  status: "Status",
  zombie: "Jim"

hash[:status] // returns "Status"
hash.status // syntax available as well

variable[:key] => value // read value from hash

In Rails, Singular and Uppercase Table Name will access lowercase and plural Table Name.

h = Hash.find(3) // finds id of 3

puts h[:id] // returns 3
puts prints to console

t = // Create new tweet
t.status = "New status"` // update db

Tweet.create // will create

t = Tweet.find(3) // Find tweet with id 3
t = Tweet.find(3, 4) // Returns an array
t = Tweet.first // returns first
t = Tweet.last // returns last
t = Tweet.all // returns all
c = Tweet.count // return number of entries

Methods can be chained:
Tweet.where(zombie: "ash").order(:status).limit(10)

t = Tweet.find(3) // First read
t.zombie = "New Value" // update // update db

Can also provide a hash on attributes:
t = Tweet.find(3) // First read
t.attributes = { status: "New Value", zombie: "Zom"} // update // update db

t.update(hash) // can also be used to update, without calling save

t = Tweet.find(3) // Find
t.destroy // deletes record


Modals how communicate with data store in Rails.

app/models/tweet.rb // singular lowercase filename

class Tweet < ActiveRecord::Base
  validates_presence_of :status
  validates_numericality_of :fingers
  validates_uniqueness_of :toothmarks
  validates_confirmation_of :password
  validates_acceptance_of :checkbox
  validates_length_of :password, minimum: 3
  validates_format_of :email, with: /regex/i
  validates_inclusion_of :age, in: 21..99
  validates_exclusion_of :age, in 0...21

  # alternate syntax
  validates :status, presence: true, length: { minimum: 3}

  has_many :tweets
  belongs_to :zombie # singular because can only belong to 1

ActiveRecord is what maps the class to the lowercase plural table

The find method returns an instance of the class with the passed in id.


.erb file: embedded ruby
<% tweet = Tweet.find(1) %> // evaluate ruby between %

<%= tweet.status %>

// = means return the value

<%= yield %> // within application.html.erb, will insert content for relevant view

<%= link_to, tweet.zombie %> link_to method to generate links
<%= link_to text_to_show, model_instance %>

<% Tweet.all.each do |tweet| %>
  <td><%= tweet.status %></td>
<% end %>
<% if tweets.size == 0 %>
  <em>No Tweets</em>
<% end %>


Conrollers where typically Use models to get data from database and views to display model.

Methods within controllers also known as actions.

TweetsController < ApplicationController
 def show
  @tweet = tweet.find(params[:id]) # use @tweet in show view to access
  render action: 'status' # render using the status.html.erb 

Instance variable (@) how controllers grants view access to varibles.

Every user gets session hash which can store things in.


config/routes.rb to configure routes.

ApplicationName::Application.routes.draw do
  root to: "tweets#index" # root route at /
  resources :tweets # standard route
  get '/new_tweet' => 'tweets#new', as: 'tweets_new'  # custom route specifiying controller method and name