Enviar pesquisa
Carregar
Desenvolvimento web com Ruby on Rails (parte 5)
•
0 gostou
•
779 visualizações
Joao Lucas Santana
Seguir
Tecnologia
Denunciar
Compartilhar
Denunciar
Compartilhar
1 de 28
Baixar agora
Baixar para ler offline
Recomendados
Desenvolvimento web com Ruby on Rails (extras)
Desenvolvimento web com Ruby on Rails (extras)
Joao Lucas Santana
Scaling Crashlytics: Building Analytics on Redis 2.6
Scaling Crashlytics: Building Analytics on Redis 2.6
Crashlytics
Mongo db presentation
Mongo db presentation
Julie Sommerville
Getting Started with MongoDB: 4 Application Designs
Getting Started with MongoDB: 4 Application Designs
DATAVERSITY
Programming Google apps with the G Suite APIs
Programming Google apps with the G Suite APIs
DevFest DC
Mongo db modifiers
Mongo db modifiers
zarigatongy
Practical MongoDB
Practical MongoDB
Will Button
Get supportedsitesjson
Get supportedsitesjson
Dửng Dừng Dưng
Recomendados
Desenvolvimento web com Ruby on Rails (extras)
Desenvolvimento web com Ruby on Rails (extras)
Joao Lucas Santana
Scaling Crashlytics: Building Analytics on Redis 2.6
Scaling Crashlytics: Building Analytics on Redis 2.6
Crashlytics
Mongo db presentation
Mongo db presentation
Julie Sommerville
Getting Started with MongoDB: 4 Application Designs
Getting Started with MongoDB: 4 Application Designs
DATAVERSITY
Programming Google apps with the G Suite APIs
Programming Google apps with the G Suite APIs
DevFest DC
Mongo db modifiers
Mongo db modifiers
zarigatongy
Practical MongoDB
Practical MongoDB
Will Button
Get supportedsitesjson
Get supportedsitesjson
Dửng Dừng Dưng
Basic crud operation
Basic crud operation
zarigatongy
The Ring programming language version 1.8 book - Part 49 of 202
The Ring programming language version 1.8 book - Part 49 of 202
Mahmoud Samir Fayed
Hi5 Opensocial Code Lab Presentation
Hi5 Opensocial Code Lab Presentation
plindner
Ushahidi
Ushahidi
bezrabotni
The Ring programming language version 1.5.1 book - Part 42 of 180
The Ring programming language version 1.5.1 book - Part 42 of 180
Mahmoud Samir Fayed
MongoD Essentials
MongoD Essentials
zahid-mian
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB Application
MongoDB
jQuery's Secrets
jQuery's Secrets
Bastian Feder
Powering Systems of Engagement
Powering Systems of Engagement
MongoDB
Advanced MongoDB #1
Advanced MongoDB #1
Takahiro Inoue
MongoDB
MongoDB
Steve Klabnik
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB
PhoneGap: Local Storage
PhoneGap: Local Storage
Ivano Malavolta
De normalised london aggregation framework overview
De normalised london aggregation framework overview
Chris Harris
MongoDB .local Munich 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pip...
MongoDB .local Munich 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pip...
MongoDB
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
South Tyrol Free Software Conference
Using Change Streams to Keep Up with Your Data
Using Change Streams to Keep Up with Your Data
Evan Rodd
The rise of json in rdbms land jab17
The rise of json in rdbms land jab17
alikonweb
1140 p2 p04_and_1350_p2p05_and_1440_p2p06
1140 p2 p04_and_1350_p2p05_and_1440_p2p06
MongoDB
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
NoSQLmatters
Critical Rendering Path - Velocidade também é uma funcionalidade
Critical Rendering Path - Velocidade também é uma funcionalidade
Joao Lucas Santana
V. Alagueva
V. Alagueva
soshi3
Mais conteúdo relacionado
Mais procurados
Basic crud operation
Basic crud operation
zarigatongy
The Ring programming language version 1.8 book - Part 49 of 202
The Ring programming language version 1.8 book - Part 49 of 202
Mahmoud Samir Fayed
Hi5 Opensocial Code Lab Presentation
Hi5 Opensocial Code Lab Presentation
plindner
Ushahidi
Ushahidi
bezrabotni
The Ring programming language version 1.5.1 book - Part 42 of 180
The Ring programming language version 1.5.1 book - Part 42 of 180
Mahmoud Samir Fayed
MongoD Essentials
MongoD Essentials
zahid-mian
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB Application
MongoDB
jQuery's Secrets
jQuery's Secrets
Bastian Feder
Powering Systems of Engagement
Powering Systems of Engagement
MongoDB
Advanced MongoDB #1
Advanced MongoDB #1
Takahiro Inoue
MongoDB
MongoDB
Steve Klabnik
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB
PhoneGap: Local Storage
PhoneGap: Local Storage
Ivano Malavolta
De normalised london aggregation framework overview
De normalised london aggregation framework overview
Chris Harris
MongoDB .local Munich 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pip...
MongoDB .local Munich 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pip...
MongoDB
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
South Tyrol Free Software Conference
Using Change Streams to Keep Up with Your Data
Using Change Streams to Keep Up with Your Data
Evan Rodd
The rise of json in rdbms land jab17
The rise of json in rdbms land jab17
alikonweb
1140 p2 p04_and_1350_p2p05_and_1440_p2p06
1140 p2 p04_and_1350_p2p05_and_1440_p2p06
MongoDB
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
NoSQLmatters
Mais procurados
(20)
Basic crud operation
Basic crud operation
The Ring programming language version 1.8 book - Part 49 of 202
The Ring programming language version 1.8 book - Part 49 of 202
Hi5 Opensocial Code Lab Presentation
Hi5 Opensocial Code Lab Presentation
Ushahidi
Ushahidi
The Ring programming language version 1.5.1 book - Part 42 of 180
The Ring programming language version 1.5.1 book - Part 42 of 180
MongoD Essentials
MongoD Essentials
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB Application
jQuery's Secrets
jQuery's Secrets
Powering Systems of Engagement
Powering Systems of Engagement
Advanced MongoDB #1
Advanced MongoDB #1
MongoDB
MongoDB
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB Performance
PhoneGap: Local Storage
PhoneGap: Local Storage
De normalised london aggregation framework overview
De normalised london aggregation framework overview
MongoDB .local Munich 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pip...
MongoDB .local Munich 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pip...
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
Using Change Streams to Keep Up with Your Data
Using Change Streams to Keep Up with Your Data
The rise of json in rdbms land jab17
The rise of json in rdbms land jab17
1140 p2 p04_and_1350_p2p05_and_1440_p2p06
1140 p2 p04_and_1350_p2p05_and_1440_p2p06
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Destaque
Critical Rendering Path - Velocidade também é uma funcionalidade
Critical Rendering Path - Velocidade também é uma funcionalidade
Joao Lucas Santana
V. Alagueva
V. Alagueva
soshi3
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Joao Lucas Santana
K dokladu
K dokladu
soshi3
anonimoys
anonimoys
john4cx
Lcd samsung sp20 so la20s51b
Lcd samsung sp20 so la20s51b
Juan Francisco Pino Rodriguez
Destaque
(6)
Critical Rendering Path - Velocidade também é uma funcionalidade
Critical Rendering Path - Velocidade também é uma funcionalidade
V. Alagueva
V. Alagueva
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
K dokladu
K dokladu
anonimoys
anonimoys
Lcd samsung sp20 so la20s51b
Lcd samsung sp20 so la20s51b
Semelhante a Desenvolvimento web com Ruby on Rails (parte 5)
Desenvolvimento web com Ruby on Rails (parte 3)
Desenvolvimento web com Ruby on Rails (parte 3)
Joao Lucas Santana
How to leverage what's new in MongoDB 3.6
How to leverage what's new in MongoDB 3.6
Maxime Beugnet
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Alex Sharp
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)
MongoSF
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
Alex Sharp
2011 05-23 metrics-agilasverige-english
2011 05-23 metrics-agilasverige-english
Mårten Gustafson
IOOF IT System Modernisation
IOOF IT System Modernisation
MongoDB
Building decentralised apps with js - Devoxx Morocco 2018
Building decentralised apps with js - Devoxx Morocco 2018
Mikhail Kuznetcov
[MongoDB.local Bengaluru 2018] Using Change Streams to Keep Up With Your Data
[MongoDB.local Bengaluru 2018] Using Change Streams to Keep Up With Your Data
MongoDB
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
Alex Sharp
Back to Basics 2017 - Your First MongoDB Application
Back to Basics 2017 - Your First MongoDB Application
Joe Drumgoole
IBM Db2 JSON 11.5
IBM Db2 JSON 11.5
Phil Downey
MongoDB Performance Tuning
MongoDB Performance Tuning
Puneet Behl
Tools and Projects Dec 2018 Edition
Tools and Projects Dec 2018 Edition
Jesus Manuel Olivas
Mongo db world 2014 billrun
Mongo db world 2014 billrun
MongoDB
MongoDB for Analytics
MongoDB for Analytics
MongoDB
Cbsecomputersciencecclass12boardproject bankmanagmentsystem-180703065625-conv...
Cbsecomputersciencecclass12boardproject bankmanagmentsystem-180703065625-conv...
sriram sarwan
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Dan Robinson
Introduction to Active Record - Silicon Valley Ruby Conference 2007
Introduction to Active Record - Silicon Valley Ruby Conference 2007
Rabble .
MongoDB
MongoDB
techwhizbang
Semelhante a Desenvolvimento web com Ruby on Rails (parte 5)
(20)
Desenvolvimento web com Ruby on Rails (parte 3)
Desenvolvimento web com Ruby on Rails (parte 3)
How to leverage what's new in MongoDB 3.6
How to leverage what's new in MongoDB 3.6
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
2011 05-23 metrics-agilasverige-english
2011 05-23 metrics-agilasverige-english
IOOF IT System Modernisation
IOOF IT System Modernisation
Building decentralised apps with js - Devoxx Morocco 2018
Building decentralised apps with js - Devoxx Morocco 2018
[MongoDB.local Bengaluru 2018] Using Change Streams to Keep Up With Your Data
[MongoDB.local Bengaluru 2018] Using Change Streams to Keep Up With Your Data
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
Back to Basics 2017 - Your First MongoDB Application
Back to Basics 2017 - Your First MongoDB Application
IBM Db2 JSON 11.5
IBM Db2 JSON 11.5
MongoDB Performance Tuning
MongoDB Performance Tuning
Tools and Projects Dec 2018 Edition
Tools and Projects Dec 2018 Edition
Mongo db world 2014 billrun
Mongo db world 2014 billrun
MongoDB for Analytics
MongoDB for Analytics
Cbsecomputersciencecclass12boardproject bankmanagmentsystem-180703065625-conv...
Cbsecomputersciencecclass12boardproject bankmanagmentsystem-180703065625-conv...
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Introduction to Active Record - Silicon Valley Ruby Conference 2007
Introduction to Active Record - Silicon Valley Ruby Conference 2007
MongoDB
MongoDB
Último
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
debabhi2
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
apidays
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
Khem
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Martijn de Jong
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Drew Madelung
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Principled Technologies
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
Pixlogix Infotech
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Enterprise Knowledge
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
The Digital Insurer
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Katpro Technologies
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
sudhanshuwaghmare1
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
Puma Security, LLC
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
HampshireHUG
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
The Digital Insurer
How to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
naman860154
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Miguel Araújo
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
Radu Cotescu
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
Delhi Call girls
Último
(20)
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
How to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
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 => "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
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: "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
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: "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
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("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">]
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("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">]
15.
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)
16.
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) }
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 %>
27.
Bills @jlucasps
28.
Desenvolvimento Web com Ruby
on Rails João Lucas Pereira de Santana gtalk | linkedin | twitter: jlucasps Obrigado!
Baixar agora