34
Ruby on Rails

Ruby on Rails Introduction

Embed Size (px)

DESCRIPTION

Ruby on Rails introduction.

Citation preview

Page 1: Ruby on Rails Introduction

Ruby on Rails

Page 2: Ruby on Rails Introduction

Introduction to Ruby

Ruby Syntax and Concepts Variables Constants Blocks Hashes

Page 3: Ruby on Rails Introduction

Rails Principles and patterns

DRY Convention Over Configuration Active Record Patern MVC

Page 4: Ruby on Rails Introduction

Model

the model - which is the layer of the system responsible for representing business data and logic

In Rails ActiveRecord is represents a Model

The interface of an object conforming to this pattern would include functions such as Insert, Update, and Delete, plus properties that correspond more or less directly to the columns in the underlying database table.

ActiveRecord is the default ‘model’ component of the model-view-controller web-application framework Ruby on Rails, and is also a stand-alone ORM package for other Ruby applications

Active Record as an ORM Framework: DataMapper and Sequel show major improvements over the original ActiveRecord framework

Page 5: Ruby on Rails Introduction

Active Record as an ORM Framework

Active Record gives us several mechanisms, the most important being the ability to:

Represent models and their data Represent associations between these models Represent inheritance hierarchies through related models Validate models before they get persisted to the database Perform database operations in an object-oriented fashion.

Page 6: Ruby on Rails Introduction

Convention over configuration

Naming Conventions Database Table - Plural with underscores separating words (e.g., book_clubs) Model Class - Singular with the first letter of each word capitalized (e.g., BookClub)

Model / Class Table / Schema

Post posts LineItem line_items

Page 7: Ruby on Rails Introduction

Creating an active record Models

SQL CREATE TABLE products ( id int(11) NOT NULL auto_increment, name varchar(255), PRIMARY KEY (id) ); class Product < ActiveRecord::Base end

Page 8: Ruby on Rails Introduction

Creating active record

user = User.create(name: "David", occupation: "Code Artist")

user = User.new user.name = "David" user.occupation = "Code Artist"

user.save

Page 9: Ruby on Rails Introduction

Reading an activerecord from database

users = User.all user = User.first david = User.find_by(name: 'David') users = User.where(name: 'David', occupation: 'Code Artist').order('created_at DESC')

Page 10: Ruby on Rails Introduction

Updating Activerecords attributes

user = User.find_by(name: 'David') user.update(name: 'Dave') Destroying an ActiveRecors user = User.find_by(name: 'David') user.destroy

Page 11: Ruby on Rails Introduction

Validation

create create! save save! update update!

Page 12: Ruby on Rails Introduction

Validations

class Person < ActiveRecord::Base validates :name, presence: true end Person.create(name: "John Doe").valid? # => true Person.create(name: nil).valid? # => false

Page 13: Ruby on Rails Introduction

Following methods skip validation call decrement! decrement_counter increment! increment_counter toggle! touch update_all update_attribute update_column update_columns update_counters

Page 14: Ruby on Rails Introduction

Validation Helpers

Acceptance class Person < ActiveRecord::Base validates :terms_of_service, acceptance: true end

validates_associated

class Library < ActiveRecord::Base has_many :books validates_associated :books end

Page 15: Ruby on Rails Introduction

Length class Person < ActiveRecord::Base validates :name, length: { minimum: 2 } validates :bio, length: { maximum: 500 } validates :password, length: { in: 6..20 } validates :registration_number, length: { is: 6 } end Uniqueness class Account < ActiveRecord::Base validates :email, uniqueness: true end

Page 16: Ruby on Rails Introduction

CallBacks

class User < ActiveRecord::Base validates :login, :email, presence: true before_validation :ensure_login_has_a_value protected def ensure_login_has_a_value if login.nil? self.login = email unless email.blank? end end end

Page 17: Ruby on Rails Introduction

Order and list of Model Call Backs

Page 18: Ruby on Rails Introduction

Migration

A DSL provided by rails to manage Database schema class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| t.string :name t.text :description t.timestamps end end end

Page 19: Ruby on Rails Introduction

Rails generator to manage migrations $ rails generate migration AddPartNumberToProducts or $ rails generate migration add_part_number_to_product part_number:number or $ rails generate model Product name:string description:text Using generator to create a migration for removing Part from products $ rails generate migration RemovePartNumberFromProducts part_number:string

Page 20: Ruby on Rails Introduction

Running a migration $ rake db:migrate Rolling Back $ rake db:rollback

Page 21: Ruby on Rails Introduction

Assocaiations

class Customer < ActiveRecord::Base end class Order < ActiveRecord::Base end @order = Order.create(order_date: Time.now, customer_id: @customer.id)

Page 22: Ruby on Rails Introduction

class Customer < ActiveRecord::Base has_many :orders, dependent: :destroy end class Order < ActiveRecord::Base belongs_to :customer end @order = @customer.orders.create(order_date: Time.now)

Page 23: Ruby on Rails Introduction

Types of association

belongs_to has_one has_many has_many :through has_one :through has_and_belongs_to_many

Page 24: Ruby on Rails Introduction

belongs_to

class Order < ActiveRecord::Base belongs_to :customer end

Page 25: Ruby on Rails Introduction

has_one

Page 26: Ruby on Rails Introduction

has_one :through

Page 27: Ruby on Rails Introduction

has_many :

Page 28: Ruby on Rails Introduction

The has_many :through Association

Page 29: Ruby on Rails Introduction

The has_and_belongs_to_many Association:

Page 30: Ruby on Rails Introduction

Controller

Page 31: Ruby on Rails Introduction

C Of MV(C) in rails

Routing: request from browser GET /patients/17

routes.rb get '/patients/:id', to: 'patients#show' resources :doctor

Page 32: Ruby on Rails Introduction

CRUD, Verbs, and Actions

Page 33: Ruby on Rails Introduction
Page 34: Ruby on Rails Introduction

Thank You