ruby on rails introduction
DESCRIPTION
Ruby on Rails introduction.TRANSCRIPT
![Page 1: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/1.jpg)
Ruby on Rails
![Page 2: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/2.jpg)
Introduction to Ruby
Ruby Syntax and Concepts Variables Constants Blocks Hashes
![Page 3: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/3.jpg)
Rails Principles and patterns
DRY Convention Over Configuration Active Record Patern MVC
![Page 4: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/4.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/5.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/6.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/7.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/8.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/9.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/10.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/11.jpg)
Validation
create create! save save! update update!
![Page 12: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/12.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/13.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/14.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/15.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/16.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/17.jpg)
Order and list of Model Call Backs
![Page 18: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/18.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/19.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/20.jpg)
Running a migration $ rake db:migrate Rolling Back $ rake db:rollback
![Page 21: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/21.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/22.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/23.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/24.jpg)
belongs_to
class Order < ActiveRecord::Base belongs_to :customer end
![Page 25: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/25.jpg)
has_one
![Page 26: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/26.jpg)
has_one :through
![Page 27: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/27.jpg)
has_many :
![Page 28: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/28.jpg)
The has_many :through Association
![Page 29: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/29.jpg)
The has_and_belongs_to_many Association:
![Page 30: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/30.jpg)
Controller
![Page 31: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/31.jpg)
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](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/32.jpg)
CRUD, Verbs, and Actions
![Page 33: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/33.jpg)
![Page 34: Ruby on Rails Introduction](https://reader038.vdocuments.site/reader038/viewer/2022103016/554f48bbb4c905423f8b4b2f/html5/thumbnails/34.jpg)
Thank You