SlideShare a Scribd company logo
Desenvolvimento
Web com Ruby on
Rails
João Lucas Pereira de Santana
gtalk | linkedin | twitter: jlucasps
Migration bills
@jlucasps
jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails g
migration CreateBills
invoke active_record
create db/migrate/20130618074102_create_bills.rb
class CreateBills < ActiveRecord::Migration
def change
create_table :bills do |t|
t.column :name, :string, :null => false
t.column :description, :string
t.references :user, :foreign_key => true
t.column :date, :datetime, :null => false
t.column :value, :decimal
t.timestamps
end
end
end
Criar arquivo de migration e executá-lo
Migration bills
@jlucasps
jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:migrate
== CreateBills: migrating
===============================================
=====
-- create_table(:bills)
-> 0.0073s
-- add_index(:bills, :user_id)
-> 0.0005s
== CreateBills: migrated (0.0080s)
===========================================
jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:rollback
== CreateBills: reverting
===============================================
=====
-- remove_index("bills", {:column=>:user_id})
-> 0.0082s
-- drop_table("bills")
-> 0.0003s
== CreateBills: reverted (0.0088s)
===========================================
Migration bills
@jlucasps
jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails g migration
AddIndexToBills
invoke active_record
create db/migrate/20130618080649_add_index_to_bills.rb
class AddIndexToBills < ActiveRecord::Migration
def change
add_index :bills, :user_id
end
end
Adicionar índice à foreign_key user_id da tabela bills
Migration bills
@jlucasps
jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:migrate
== AddIndexToBills: migrating
================================================
-- add_index(:bills, :user_id)
-> 0.0013s
== AddIndexToBills: migrated (0.0015s)
=======================================
jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:rollback
== AddIndexToBills: reverting
================================================
-- remove_index("bills", {:column=>:user_id})
-> 0.0306s
== AddIndexToBills: reverted (0.0307s)
=======================================
Model Bill
@jlucasps
class Bill < ActiveRecord::Base
# Attrs accessible
attr_accessible :name, :description, :user_id, :date, :value
# Validations
validates :name, :presence => true, :allow_blank => false
validates :user_id, :presence => true
validates :date, :presence => true
validates :value, :presence => true
# Associations
belongs_to :user
# Scopes
default_scope order("bills.date DESC")
# Públic methods
end
Model User
Alterar o model User para conter a associação com Bill
@jlucasps
class User < ActiveRecord::Base
# Attrs accessible
attr_accessible :name, :email, :age, :gender
# Constants
MALE = 1
FEMALE = 2
OTHER = 3
# Validations
validates :name, :presence => true, :allow_blank => false
validates :email, :presence => true, :allow_blank => false
validates_uniqueness_of :email
validates :gender, :presence => true, :if => :adulthood
# Associations
has_many :bills, :dependent => :destroy
# Scopes
# Públic methods
def adulthood
self.age.present? and age >= 18
end
end
Rails console
@jlucasps
jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails console
Loading development environment (Rails 3.2.13)
irb(main):001:0> user = User.new(:name => "Joao Lucas", :email =>
"jlucasps@gmail.com")
=> #<User id: nil, name: "Joao Lucas", email: "jlucasps@gmail.com", age: nil,
created_at: nil, updated_at: nil, gender: nil>
irb(main):002:0> user.save
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'jlucasps@gmail.com' LIMIT
1
SQL (24.1ms) INSERT INTO "users" ("age", "created_at", "email", "gender", "name", "updated_at")
VALUES (?, ?, ?, ?, ?, ?) [["age", nil], ["created_at", Tue, 18 Jun 2013 12:52:30 UTC +00:00], ["email",
"jlucasps@gmail.com"], ["gender", nil], ["name", "Joao Lucas"], ["updated_at", Tue, 18 Jun 2013 12:52:
30 UTC +00:00]]
(422.3ms) commit transaction
=> true
irb(main):003:0>
Criar um usuário utilizando o $ rails console
Rails console
Criar 2 objetos Bill e associá-los a um usuário.
@jlucasps
irb(main):004:0> bill_1 = Bill.new :name => "Conta 1", :date => Time.
utc(2012, 06, 18), :value => 48.90
=> #<Bill id: nil, name: "Conta 1", description: nil, user_id: nil, date: "2012-06-
18 00:00:00", value: #<BigDecimal:3fc45d8,'0.489E2',18(45)>, created_at: nil,
updated_at: nil>
irb(main):012:0> bill_1.user = user
=> #<User id: 7, name: "Joao Lucas", email: "jlucasps@gmail.com", age: nil,
created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30",
gender: nil>
irb(main):013:0> bill_1.save
(0.1ms) begin transaction
SQL (7.7ms) INSERT INTO "bills" ("created_at", "date", "description", "name", "updated_at", "user_id",
"value") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Tue, 18 Jun 2013 13:03:13 UTC +00:00], ["date", Mon,
18 Jun 2012 00:00:00 UTC +00:00], ["description", nil], ["name", "Conta 1"], ["updated_at", Tue, 18 Jun
2013 13:03:13 UTC +00:00], ["user_id", 7], ["value", #<BigDecimal:31e9878,'0.489E2',18(45)>]]
(413.9ms) commit transaction
=> true
Rails console
@jlucasps
irb(main):014:0> bill_2 = Bill.new :name => "Conta 2", :date => Time.
utc(2012, 06, 17), :value => 31.50
=> #<Bill id: nil, name: "Conta 2", description: nil, user_id: nil, date: "2012-06-
17 00:00:00", value: #<BigDecimal:437f150,'0.315E2',18(45)>, created_at: nil,
updated_at: nil>
irb(main):015:0> bill_2.user = user
=> #<User id: 7, name: "Joao Lucas", email: "jlucasps@gmail.com", age: nil,
created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30",
gender: nil>
irb(main):016:0> bill_2.save
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO "bills" ("created_at", "date", "description", "name", "updated_at", "user_id",
"value") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Tue, 18 Jun 2013 13:05:13 UTC +00:00], ["date", Sun,
17 Jun 2012 00:00:00 UTC +00:00], ["description", nil], ["name", "Conta 2"], ["updated_at", Tue, 18 Jun
2013 13:05:13 UTC +00:00], ["user_id", 7], ["value", #<BigDecimal:3c1e0f0,'0.315E2',18(45)>]]
(427.6ms) commit transaction
=> true
Rails console
@jlucasps
irb(main):017:0> Bill.find_all_by_user_id(user.id)
Bill Load (0.3ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7
ORDER BY bills.date DESC
=> [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18
00:00:00", value: #<BigDecimal:46f1428,'0.489E2',18(45)>, created_at: "2013-
06-18 13:03:13", updated_at: "2013-06-18 13:03:13">, #<Bill id: 2, name:
"Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value:
#<BigDecimal:46f8048,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13",
updated_at: "2013-06-18 13:05:13">]
irb(main):018:0> Bill.find_by_user_id user.id
Bill Load (0.5ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7
ORDER BY bills.date DESC LIMIT 1
=> #<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18
00:00:00", value: #<BigDecimal:4706260,'0.489E2',18(45)>, created_at: "2013-
06-18 13:03:13", updated_at: "2013-06-18 13:03:13">
irb(main):019:0>
Rails console
@jlucasps
irb(main):020:0> Bill.where(:user_id => user.id).sum(:value)
(0.2ms) SELECT SUM("bills"."value") AS sum_id FROM "bills" WHERE
"bills"."user_id" = 7
=> #<BigDecimal:45230d8,'0.8040000000 000001E2',27(45)>
irb(main):022:0> Bill.where(:user_id => user.id, :date => Time.
utc(2012, 06, 17)).sum(:value)
(0.3ms) SELECT SUM("bills"."value") AS sum_id FROM "bills" WHERE
"bills"."user_id" = 7 AND "bills"."date" = '2012-06-17 00:00:00.000000'
=> #<BigDecimal:4a95c78,'0.315E2',18(45)>
irb(main):029:0> Bill.where(:user_id => user.id).where("bills.
date <= '2012-06-17 00:00:00.000000'").sum(:value)
(0.3ms) SELECT SUM("bills"."value") AS sum_id FROM "bills" WHERE
"bills"."user_id" = 7 AND (bills.date <= '2012-06-17 00:00:00.000000')
=> #<BigDecimal:49b44d0,'0.315E2',18(45)>
Scopes
@jlucasps
# Scopes
default_scope order("bills.date DESC")
irb(main):039:0> reload!
irb(main):040:0> user = User.find_by_email("jlucasps@gmail.com")
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" =
'jlucasps@gmail.com' LIMIT 1
=> #<User id: 7, name: "Joao Lucas", email: "jlucasps@gmail.com", age: nil,
created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30",
gender: nil>
irb(main):041:0> user.bills
Bill Load (0.2ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7
ORDER BY bills.date DESC
=> [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-
06-18 00:00:00", value: #<BigDecimal:2d11fd0,'0.489E2',18(45)>,
created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">,
#<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17
00:00:00", value: #<BigDecimal:3d120b0,'0.315E2',18(45)>, created_at:
"2013-06-18 13:05:13", updated_at: "2013-06-18 13:05:13">]
Scopes
@jlucasps
# Scopes
default_scope order("bills.date ASC")
irb(main):044:0> reload!
Reloading...
=> true
irb(main):045:0> user = User.find_by_email("jlucasps@gmail.com")
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" =
'jlucasps@gmail.com' LIMIT 1
=> #<User id: 7, name: "Joao Lucas", email: "jlucasps@gmail.com", age: nil, created_at:
"2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil>
irb(main):046:0> user.bills
Bill Load (0.1ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY
bills.date ASC
=> [#<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:
00:00", value: #<BigDecimal:4971270,'0.315E2',18(45)>, created_at: "2013-06-18 13:
05:13", updated_at: "2013-06-18 13:05:13">, #<Bill id: 1, name: "Conta 1", description:
nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:4977eb8,'0.489E2',
18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">]
Scopes
@jlucasps
irb(main):051:0> reload!
irb(main):052:0> user = User.find_by_email("jlucasps@gmail.com")
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'jlucasps@gmail.com' LIMIT 1
=> #<User id: 7, name: "Joao Lucas", email: "jlucasps@gmail.com", age: nil, created_at: "2013-06-18 12:
52:30", updated_at: "2013-06-18 12:52:30", gender: nil>
irb(main):053:0> user.bills
Bill Load (0.1ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC
=> [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value:
#<BigDecimal:49105b0,'0.489E2',18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18
13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value:
#<BigDecimal:491b050,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18
13:05:13">]
irb(main):054:0> user.bills.recents
Bill Load (0.4ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC
LIMIT 5
=> [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value:
#<BigDecimal:4938560,'0.489E2',18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18
13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value:
#<BigDecimal:49368f0,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18
13:05:13">]
# Scopes
default_scope order("bills.date DESC")
scope :recents, order("bills.date DESC").limit(5)
Scopes
@jlucasps
irb(main):058:0> reload!
irb(main):059:0> user = User.find_by_email("jlucasps@gmail.com")
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" =
'jlucasps@gmail.com' LIMIT 1
=> #<User id: 7, name: "Joao Lucas", email: "jlucasps@gmail.com", age: nil,
created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender:
nil>
irb(main):060:0> user.bills.recents_by_date Time.utc(2012, 06, 17)
Bill Load (0.3ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 AND
(bills.date <= '2012-06-17 00:00:00.000000') ORDER BY bills.date DESC LIMIT 5
=> [#<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17
00:00:00", value: #<BigDecimal:4b07e90,'0.315E2',18(45)>, created_at: "2013-
06-18 13:05:13", updated_at: "2013-06-18 13:05:13">]
scope :recents, order("bills.date DESC").limit(5)
scope :recents_by_date, lambda {|date|
where("bills.date <= '#{date.strftime('%Y-%m-%d %H:%M:%S.000000')}'").order
("bills.date DESC").limit(5)
}
Commit
$ git add .
$ git commit -m "Criação de scopes"
Agora vamos adicionar relacionamentos entre
as rotas de Usuários e Contas
@jlucasps
...
resources :users do
resources :bills
end
...
Bills
@jlucasps
Alterar view de show do modelo user: /app/views/users/show.html.erb
<p id="notice"><%= notice %></p>
<p><b>Nome:</b><%= @user.name %></p>
<p><b>email:</b><%= @user.email %></p>
<p><b>Idade:</b><%= @user.age %></p>
<p><b>Sexo:</b><%= @user.gender %></p>
<div id='new_bill'>
<%= render :partial => 'bills/new', :locals => {:bill => Bill.new, :user => @user} %>
</div>
<div id='bills'>
<%= render :partial => 'bills/index', :locals => {:bills => @user.bills} %>
</div>
<%= link_to 'Edit', edit_user_path(@user), :class => "btn" %> |
<%= link_to 'Back', users_path, :class => "btn" %>
Bills
@jlucasps
Criar partial para lista contas: /app/views/bills/_index.html.erb
<% if bills.any? %>
<% bills.each do |bill| %>
<div class='well well-small'>
<h4><%= bill.name %></h4>
<%= bill.description %>
<h5><%= l(bill.date, :forma => :long) %></h5>
<h3><%= bill.value.round(2) %></h3>
</div>
<% end %>
<% else %>
<div class='alert'>
<%= t('no_bill_registered_to_this_user') %>
</div>
<% end %>
Bills
@jlucasps
Criar partial com formulário de nova conta: /app/views/bills/_new.html.erb
<hr>
<h4><%= t('new_bill') %></h4>
<%= form_for([user, bill], :remote => true) do |f| %>
<%= render :partial => 'shared/error_messages' , :locals => {:resource => bill} %
>
<%= f.hidden_field :user_id, :value => user.id %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :date %>
<%= f.text_field :date %>
<%= f.label :value %>
<%= f.text_field :value %>
<% end %>
Bills
@jlucasps
Criar controller bills e action create
class BillsController < ApplicationController
def create
@bill = Bill.new(params[:bill])
flash[:notice] = (@bill.save ? t('saved_successfully') : t('error_while_saving'))
end
end
Bills
@jlucasps
Criar view create /app/views/bills/create.js.erb
<% if @bill.persisted? %>
$("#bills").html("<%= j(render :partial => 'bills/index', :locals => {:
bills => @bill.user.bills}) %>");
$("#bills").prepend("<%= j notice %>");
$("#new_bill").html("<%= j(render :partial => 'bills/new', :locals =>
{:bill => Bill.new, :user => @bill.user} ) %>");
<% else %>
$("#new_bill").html("<%= j(render :partial => 'bills/new', :locals =>
{:bill => @bill, :user => @bill.user}) %>");
<% end %>
Bills
@jlucasps
Alterar view de index para apresentar link para edit e destroy
<hr>
<h4><%= t('bills') %></h4>
<% if bills.any? %>
<% bills.each do |bill| %>
<div id='bill_<%= bill.id %>' class='well well-small'>
<h4><%= bill.name %></h4>
<%= bill.description %>
<h5><%= l(bill.date, :forma => :long) %></h5>
<h3><%= bill.value.round(2) %></h3>
<%= link_to t('edit'), edit_user_bill_path(bill.user, bill), :class => "btn btn-
primary", :remote => true %>
<%= link_to t('delete'), [bill.user, bill], :remote => true, :method => :delete, :
class => "btn btn-danger" %>
</div>
<% end %>
<% else %>
<div class='alert'>
<%= t('no_bill_registered_to_this_user') %>
</div>
<% end %>
Bills
@jlucasps
Criar action e view destroy
def destroy
@bill = Bill.find(params[:id])
@bill.destroy
end
<% if @bill.destroyed? %>
$("#bill_<%= @bill.id %>").remove();
<% else %>
$("#bill_<%= @bill.id %>").prepend("<%= t
('error_while_deleting') %>");
<% end %>
Bills
@jlucasps
Criar action e view edit
def edit
@bill = Bill.find(params[:id])
end
$("#new_bill").html("<%= j(render :partial => 'bills/new', :
locals => {:bill => @bill, :user => @bill.user}) %>");
$('html, body').animate({scrollTop: $("#new_bill").offset().
top});
Bills
@jlucasps
Implementar action e view de update
def update
@bill = Bill.find(params[:id])
flash[:notice] = (@bill.update_attributes(params[:bill]) ? t
('updated_successfully') : t('error_while_updating'))
end
<% if @bill.valid? %>
$("#bills").html("<%= j(render :partial => 'bills/index', :locals => {:bills =>
@bill.user.bills}) %>");
$("#bills").prepend("<%= j notice %>");
$("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill
=> Bill.new, :user => @bill.user} ) %>");
<% else %>
$("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill
=> @bill, :user => @bill.user}) %>");
<% end %>
Bills
@jlucasps
Desenvolvimento
Web com Ruby on
Rails
João Lucas Pereira de Santana
gtalk | linkedin | twitter: jlucasps
Obrigado!

More Related Content

PDF
Desenvolvimento web com Ruby on Rails (extras)
PDF
Scaling Crashlytics: Building Analytics on Redis 2.6
KEY
Mongo db presentation
PDF
Getting Started with MongoDB: 4 Application Designs
PDF
Programming Google apps with the G Suite APIs
PPTX
Mongo db modifiers
PPTX
Practical MongoDB
TXT
Get supportedsitesjson
Desenvolvimento web com Ruby on Rails (extras)
Scaling Crashlytics: Building Analytics on Redis 2.6
Mongo db presentation
Getting Started with MongoDB: 4 Application Designs
Programming Google apps with the G Suite APIs
Mongo db modifiers
Practical MongoDB
Get supportedsitesjson

What's hot (20)

PPTX
Basic crud operation
PDF
The Ring programming language version 1.8 book - Part 49 of 202
PPT
Hi5 Opensocial Code Lab Presentation
PDF
Ushahidi
PDF
The Ring programming language version 1.5.1 book - Part 42 of 180
PDF
MongoD Essentials
PPTX
Back to Basics: My First MongoDB Application
PDF
jQuery's Secrets
PPTX
Powering Systems of Engagement
PDF
Advanced MongoDB #1
KEY
MongoDB
PDF
MongoDB Europe 2016 - Debugging MongoDB Performance
PDF
PhoneGap: Local Storage
PDF
De normalised london aggregation framework overview
PDF
MongoDB .local Munich 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pip...
PDF
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
PPTX
Using Change Streams to Keep Up with Your Data
PPTX
The rise of json in rdbms land jab17
POTX
1140 p2 p04_and_1350_p2p05_and_1440_p2p06
PDF
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Basic crud operation
The Ring programming language version 1.8 book - Part 49 of 202
Hi5 Opensocial Code Lab Presentation
Ushahidi
The Ring programming language version 1.5.1 book - Part 42 of 180
MongoD Essentials
Back to Basics: My First MongoDB Application
jQuery's Secrets
Powering Systems of Engagement
Advanced MongoDB #1
MongoDB
MongoDB Europe 2016 - Debugging MongoDB Performance
PhoneGap: Local Storage
De normalised london aggregation framework overview
MongoDB .local Munich 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pip...
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
Using Change Streams to Keep Up with Your Data
The rise of json in rdbms land jab17
1140 p2 p04_and_1350_p2p05_and_1440_p2p06
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Ad

Viewers also liked (6)

PDF
Critical Rendering Path - Velocidade também é uma funcionalidade
PPSX
V. Alagueva
PDF
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
PPT
K dokladu
ODP
anonimoys
PDF
Lcd samsung sp20 so la20s51b
Critical Rendering Path - Velocidade também é uma funcionalidade
V. Alagueva
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
K dokladu
anonimoys
Lcd samsung sp20 so la20s51b
Ad

Similar to Desenvolvimento web com Ruby on Rails (parte 5) (20)

PDF
Desenvolvimento web com Ruby on Rails (parte 3)
PPTX
How to leverage what's new in MongoDB 3.6
PDF
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
KEY
Practical Ruby Projects (Alex Sharp)
KEY
Practical Ruby Projects with MongoDB - MongoSF
KEY
2011 05-23 metrics-agilasverige-english
PPTX
IOOF IT System Modernisation
PDF
Building decentralised apps with js - Devoxx Morocco 2018
PPTX
[MongoDB.local Bengaluru 2018] Using Change Streams to Keep Up With Your Data
KEY
Practical Ruby Projects With Mongo Db
PPTX
Back to Basics 2017 - Your First MongoDB Application
PPTX
IBM Db2 JSON 11.5
PDF
MongoDB Performance Tuning
PDF
Tools and Projects Dec 2018 Edition
PDF
Mongo db world 2014 billrun
PDF
MongoDB for Analytics
DOCX
Cbsecomputersciencecclass12boardproject bankmanagmentsystem-180703065625-conv...
PPTX
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
PDF
Introduction to Active Record - Silicon Valley Ruby Conference 2007
PDF
MongoDB
Desenvolvimento web com Ruby on Rails (parte 3)
How to leverage what's new in MongoDB 3.6
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects with MongoDB - MongoSF
2011 05-23 metrics-agilasverige-english
IOOF IT System Modernisation
Building decentralised apps with js - Devoxx Morocco 2018
[MongoDB.local Bengaluru 2018] Using Change Streams to Keep Up With Your Data
Practical Ruby Projects With Mongo Db
Back to Basics 2017 - Your First MongoDB Application
IBM Db2 JSON 11.5
MongoDB Performance Tuning
Tools and Projects Dec 2018 Edition
Mongo db world 2014 billrun
MongoDB for Analytics
Cbsecomputersciencecclass12boardproject bankmanagmentsystem-180703065625-conv...
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Introduction to Active Record - Silicon Valley Ruby Conference 2007
MongoDB

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Modernizing your data center with Dell and AMD
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Advanced IT Governance
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Advanced Soft Computing BINUS July 2025.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Empathic Computing: Creating Shared Understanding
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
“AI and Expert System Decision Support & Business Intelligence Systems”
Modernizing your data center with Dell and AMD
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
NewMind AI Monthly Chronicles - July 2025
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Advanced IT Governance
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Advanced Soft Computing BINUS July 2025.pdf
The AUB Centre for AI in Media Proposal.docx
Understanding_Digital_Forensics_Presentation.pptx
Unlocking AI with Model Context Protocol (MCP)
Empathic Computing: Creating Shared Understanding
GamePlan Trading System Review: Professional Trader's Honest Take
Mobile App Security Testing_ A Comprehensive Guide.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm

Desenvolvimento web com Ruby on Rails (parte 5)

  • 1. Desenvolvimento Web com Ruby on Rails João Lucas Pereira de Santana gtalk | linkedin | twitter: jlucasps
  • 2. Migration bills @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails g migration CreateBills invoke active_record create db/migrate/20130618074102_create_bills.rb class CreateBills < ActiveRecord::Migration def change create_table :bills do |t| t.column :name, :string, :null => false t.column :description, :string t.references :user, :foreign_key => true t.column :date, :datetime, :null => false t.column :value, :decimal t.timestamps end end end Criar arquivo de migration e executá-lo
  • 3. Migration bills @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:migrate == CreateBills: migrating =============================================== ===== -- create_table(:bills) -> 0.0073s -- add_index(:bills, :user_id) -> 0.0005s == CreateBills: migrated (0.0080s) =========================================== jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:rollback == CreateBills: reverting =============================================== ===== -- remove_index("bills", {:column=>:user_id}) -> 0.0082s -- drop_table("bills") -> 0.0003s == CreateBills: reverted (0.0088s) ===========================================
  • 4. Migration bills @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails g migration AddIndexToBills invoke active_record create db/migrate/20130618080649_add_index_to_bills.rb class AddIndexToBills < ActiveRecord::Migration def change add_index :bills, :user_id end end Adicionar índice à foreign_key user_id da tabela bills
  • 5. Migration bills @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:migrate == AddIndexToBills: migrating ================================================ -- add_index(:bills, :user_id) -> 0.0013s == AddIndexToBills: migrated (0.0015s) ======================================= jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:rollback == AddIndexToBills: reverting ================================================ -- remove_index("bills", {:column=>:user_id}) -> 0.0306s == AddIndexToBills: reverted (0.0307s) =======================================
  • 6. Model Bill @jlucasps class Bill < ActiveRecord::Base # Attrs accessible attr_accessible :name, :description, :user_id, :date, :value # Validations validates :name, :presence => true, :allow_blank => false validates :user_id, :presence => true validates :date, :presence => true validates :value, :presence => true # Associations belongs_to :user # Scopes default_scope order("bills.date DESC") # Públic methods end
  • 7. Model User Alterar o model User para conter a associação com Bill @jlucasps class User < ActiveRecord::Base # Attrs accessible attr_accessible :name, :email, :age, :gender # Constants MALE = 1 FEMALE = 2 OTHER = 3 # Validations validates :name, :presence => true, :allow_blank => false validates :email, :presence => true, :allow_blank => false validates_uniqueness_of :email validates :gender, :presence => true, :if => :adulthood # Associations has_many :bills, :dependent => :destroy # Scopes # Públic methods def adulthood self.age.present? and age >= 18 end end
  • 8. Rails console @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails console Loading development environment (Rails 3.2.13) irb(main):001:0> user = User.new(:name => "Joao Lucas", :email => "[email protected]") => #<User id: nil, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: nil, updated_at: nil, gender: nil> irb(main):002:0> user.save (0.1ms) begin transaction User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 SQL (24.1ms) INSERT INTO "users" ("age", "created_at", "email", "gender", "name", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["age", nil], ["created_at", Tue, 18 Jun 2013 12:52:30 UTC +00:00], ["email", "[email protected]"], ["gender", nil], ["name", "Joao Lucas"], ["updated_at", Tue, 18 Jun 2013 12:52: 30 UTC +00:00]] (422.3ms) commit transaction => true irb(main):003:0> Criar um usuário utilizando o $ rails console
  • 9. Rails console Criar 2 objetos Bill e associá-los a um usuário. @jlucasps irb(main):004:0> bill_1 = Bill.new :name => "Conta 1", :date => Time. utc(2012, 06, 18), :value => 48.90 => #<Bill id: nil, name: "Conta 1", description: nil, user_id: nil, date: "2012-06- 18 00:00:00", value: #<BigDecimal:3fc45d8,'0.489E2',18(45)>, created_at: nil, updated_at: nil> irb(main):012:0> bill_1.user = user => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):013:0> bill_1.save (0.1ms) begin transaction SQL (7.7ms) INSERT INTO "bills" ("created_at", "date", "description", "name", "updated_at", "user_id", "value") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Tue, 18 Jun 2013 13:03:13 UTC +00:00], ["date", Mon, 18 Jun 2012 00:00:00 UTC +00:00], ["description", nil], ["name", "Conta 1"], ["updated_at", Tue, 18 Jun 2013 13:03:13 UTC +00:00], ["user_id", 7], ["value", #<BigDecimal:31e9878,'0.489E2',18(45)>]] (413.9ms) commit transaction => true
  • 10. Rails console @jlucasps irb(main):014:0> bill_2 = Bill.new :name => "Conta 2", :date => Time. utc(2012, 06, 17), :value => 31.50 => #<Bill id: nil, name: "Conta 2", description: nil, user_id: nil, date: "2012-06- 17 00:00:00", value: #<BigDecimal:437f150,'0.315E2',18(45)>, created_at: nil, updated_at: nil> irb(main):015:0> bill_2.user = user => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):016:0> bill_2.save (0.1ms) begin transaction SQL (0.7ms) INSERT INTO "bills" ("created_at", "date", "description", "name", "updated_at", "user_id", "value") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Tue, 18 Jun 2013 13:05:13 UTC +00:00], ["date", Sun, 17 Jun 2012 00:00:00 UTC +00:00], ["description", nil], ["name", "Conta 2"], ["updated_at", Tue, 18 Jun 2013 13:05:13 UTC +00:00], ["user_id", 7], ["value", #<BigDecimal:3c1e0f0,'0.315E2',18(45)>]] (427.6ms) commit transaction => true
  • 11. Rails console @jlucasps irb(main):017:0> Bill.find_all_by_user_id(user.id) Bill Load (0.3ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC => [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:46f1428,'0.489E2',18(45)>, created_at: "2013- 06-18 13:03:13", updated_at: "2013-06-18 13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:46f8048,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18 13:05:13">] irb(main):018:0> Bill.find_by_user_id user.id Bill Load (0.5ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC LIMIT 1 => #<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:4706260,'0.489E2',18(45)>, created_at: "2013- 06-18 13:03:13", updated_at: "2013-06-18 13:03:13"> irb(main):019:0>
  • 12. Rails console @jlucasps irb(main):020:0> Bill.where(:user_id => user.id).sum(:value) (0.2ms) SELECT SUM("bills"."value") AS sum_id FROM "bills" WHERE "bills"."user_id" = 7 => #<BigDecimal:45230d8,'0.8040000000 000001E2',27(45)> irb(main):022:0> Bill.where(:user_id => user.id, :date => Time. utc(2012, 06, 17)).sum(:value) (0.3ms) SELECT SUM("bills"."value") AS sum_id FROM "bills" WHERE "bills"."user_id" = 7 AND "bills"."date" = '2012-06-17 00:00:00.000000' => #<BigDecimal:4a95c78,'0.315E2',18(45)> irb(main):029:0> Bill.where(:user_id => user.id).where("bills. date <= '2012-06-17 00:00:00.000000'").sum(:value) (0.3ms) SELECT SUM("bills"."value") AS sum_id FROM "bills" WHERE "bills"."user_id" = 7 AND (bills.date <= '2012-06-17 00:00:00.000000') => #<BigDecimal:49b44d0,'0.315E2',18(45)>
  • 13. Scopes @jlucasps # Scopes default_scope order("bills.date DESC") irb(main):039:0> reload! irb(main):040:0> user = User.find_by_email("[email protected]") User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):041:0> user.bills Bill Load (0.2ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC => [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012- 06-18 00:00:00", value: #<BigDecimal:2d11fd0,'0.489E2',18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:3d120b0,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18 13:05:13">]
  • 14. Scopes @jlucasps # Scopes default_scope order("bills.date ASC") irb(main):044:0> reload! Reloading... => true irb(main):045:0> user = User.find_by_email("[email protected]") User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):046:0> user.bills Bill Load (0.1ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date ASC => [#<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00: 00:00", value: #<BigDecimal:4971270,'0.315E2',18(45)>, created_at: "2013-06-18 13: 05:13", updated_at: "2013-06-18 13:05:13">, #<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:4977eb8,'0.489E2', 18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">]
  • 15. Scopes @jlucasps irb(main):051:0> reload! irb(main):052:0> user = User.find_by_email("[email protected]") User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12: 52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):053:0> user.bills Bill Load (0.1ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC => [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:49105b0,'0.489E2',18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:491b050,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18 13:05:13">] irb(main):054:0> user.bills.recents Bill Load (0.4ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC LIMIT 5 => [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:4938560,'0.489E2',18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:49368f0,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18 13:05:13">] # Scopes default_scope order("bills.date DESC") scope :recents, order("bills.date DESC").limit(5)
  • 16. Scopes @jlucasps irb(main):058:0> reload! irb(main):059:0> user = User.find_by_email("[email protected]") User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):060:0> user.bills.recents_by_date Time.utc(2012, 06, 17) Bill Load (0.3ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 AND (bills.date <= '2012-06-17 00:00:00.000000') ORDER BY bills.date DESC LIMIT 5 => [#<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:4b07e90,'0.315E2',18(45)>, created_at: "2013- 06-18 13:05:13", updated_at: "2013-06-18 13:05:13">] scope :recents, order("bills.date DESC").limit(5) scope :recents_by_date, lambda {|date| where("bills.date <= '#{date.strftime('%Y-%m-%d %H:%M:%S.000000')}'").order ("bills.date DESC").limit(5) }
  • 17. Commit $ git add . $ git commit -m "Criação de scopes" Agora vamos adicionar relacionamentos entre as rotas de Usuários e Contas @jlucasps ... resources :users do resources :bills end ...
  • 18. Bills @jlucasps Alterar view de show do modelo user: /app/views/users/show.html.erb <p id="notice"><%= notice %></p> <p><b>Nome:</b><%= @user.name %></p> <p><b>email:</b><%= @user.email %></p> <p><b>Idade:</b><%= @user.age %></p> <p><b>Sexo:</b><%= @user.gender %></p> <div id='new_bill'> <%= render :partial => 'bills/new', :locals => {:bill => Bill.new, :user => @user} %> </div> <div id='bills'> <%= render :partial => 'bills/index', :locals => {:bills => @user.bills} %> </div> <%= link_to 'Edit', edit_user_path(@user), :class => "btn" %> | <%= link_to 'Back', users_path, :class => "btn" %>
  • 19. Bills @jlucasps Criar partial para lista contas: /app/views/bills/_index.html.erb <% if bills.any? %> <% bills.each do |bill| %> <div class='well well-small'> <h4><%= bill.name %></h4> <%= bill.description %> <h5><%= l(bill.date, :forma => :long) %></h5> <h3><%= bill.value.round(2) %></h3> </div> <% end %> <% else %> <div class='alert'> <%= t('no_bill_registered_to_this_user') %> </div> <% end %>
  • 20. Bills @jlucasps Criar partial com formulário de nova conta: /app/views/bills/_new.html.erb <hr> <h4><%= t('new_bill') %></h4> <%= form_for([user, bill], :remote => true) do |f| %> <%= render :partial => 'shared/error_messages' , :locals => {:resource => bill} % > <%= f.hidden_field :user_id, :value => user.id %> <%= f.label :name %> <%= f.text_field :name %> <%= f.label :description %> <%= f.text_field :description %> <%= f.label :date %> <%= f.text_field :date %> <%= f.label :value %> <%= f.text_field :value %> <% end %>
  • 21. Bills @jlucasps Criar controller bills e action create class BillsController < ApplicationController def create @bill = Bill.new(params[:bill]) flash[:notice] = (@bill.save ? t('saved_successfully') : t('error_while_saving')) end end
  • 22. Bills @jlucasps Criar view create /app/views/bills/create.js.erb <% if @bill.persisted? %> $("#bills").html("<%= j(render :partial => 'bills/index', :locals => {: bills => @bill.user.bills}) %>"); $("#bills").prepend("<%= j notice %>"); $("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill => Bill.new, :user => @bill.user} ) %>"); <% else %> $("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill => @bill, :user => @bill.user}) %>"); <% end %>
  • 23. Bills @jlucasps Alterar view de index para apresentar link para edit e destroy <hr> <h4><%= t('bills') %></h4> <% if bills.any? %> <% bills.each do |bill| %> <div id='bill_<%= bill.id %>' class='well well-small'> <h4><%= bill.name %></h4> <%= bill.description %> <h5><%= l(bill.date, :forma => :long) %></h5> <h3><%= bill.value.round(2) %></h3> <%= link_to t('edit'), edit_user_bill_path(bill.user, bill), :class => "btn btn- primary", :remote => true %> <%= link_to t('delete'), [bill.user, bill], :remote => true, :method => :delete, : class => "btn btn-danger" %> </div> <% end %> <% else %> <div class='alert'> <%= t('no_bill_registered_to_this_user') %> </div> <% end %>
  • 24. Bills @jlucasps Criar action e view destroy def destroy @bill = Bill.find(params[:id]) @bill.destroy end <% if @bill.destroyed? %> $("#bill_<%= @bill.id %>").remove(); <% else %> $("#bill_<%= @bill.id %>").prepend("<%= t ('error_while_deleting') %>"); <% end %>
  • 25. Bills @jlucasps Criar action e view edit def edit @bill = Bill.find(params[:id]) end $("#new_bill").html("<%= j(render :partial => 'bills/new', : locals => {:bill => @bill, :user => @bill.user}) %>"); $('html, body').animate({scrollTop: $("#new_bill").offset(). top});
  • 26. Bills @jlucasps Implementar action e view de update def update @bill = Bill.find(params[:id]) flash[:notice] = (@bill.update_attributes(params[:bill]) ? t ('updated_successfully') : t('error_while_updating')) end <% if @bill.valid? %> $("#bills").html("<%= j(render :partial => 'bills/index', :locals => {:bills => @bill.user.bills}) %>"); $("#bills").prepend("<%= j notice %>"); $("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill => Bill.new, :user => @bill.user} ) %>"); <% else %> $("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill => @bill, :user => @bill.user}) %>"); <% end %>
  • 28. Desenvolvimento Web com Ruby on Rails João Lucas Pereira de Santana gtalk | linkedin | twitter: jlucasps Obrigado!