SlideShare uma empresa Scribd logo
1 de 11
10 Ruby and Rails Pro Tips
Michel Pigassou, Co-founder & CTO at Fidzup, michel@fidzup.com
2014-08-05
A few things you may or may not know
Use debugger
Just use it!
How? http://guides.rubyonrails.org/debugging_rails_applications.html
Bonus:
byebug for Ruby 2: https://github.com/deivid-rodriguez/byebug
Inline Assignments
Don't:
params[:routing_data] ? routing_data = params[:routing_data].to_json :
routing_data = {}
Do:
routing_data = params[:routing_data] ? params[:routing_data].to_json : {}
Fail Fast
Integer() and Float() raise an Exception
Also Hash#fetch
Bonus: Array()
Use Constants and Integers
Example:
CHOICES = [:female, :male, :unknown].freeze
user.sex = CHOICES.index(:female)
user.sex = CHOICES[0]
Be Careful with =~
=~ returns an integer or nil
("foo" =~ /foo/) === true #=> false
Eliminate Tautologies
Don't:
<%= radio_button_tag 'date_field',
'created_at',
params[:date_field] == 'created_at' ? true : false
%>
Do:
<%= radio_button_tag 'date_field',
'created_at',
params[:date_field] == 'created_at' %>
Use update_column
update_attribute runs callbacks
update_column doesn't
Avoid nested_attributes
Complicated with AJAX and deep nested
associations
Alternatives:
- Do It Yourself
- Redtape (https://github.com/ClearFit/redtape)
Useful Gems
Annotate (Schema in models) https://github.com/ctran/annotate_models
Lograge (Better logs) https://github.com/roidrage/lograge
factory_girl (Better fixtures) https://github.com/thoughtbot/factory_girl
better_errors https://github.com/charliesome/better_errors
Let the DB Do its Work
Filling dates without value:
WITH filled_dates as (
select day, 0 as blank_count from generate_series('#{@start_date}', current_date::date, '1 day') as day
),
stats as (
SELECT COUNT(*) AS count, date_trunc('day', created_at) AS day
FROM "checkins" WHERE
(created_at >= '#{@start_date.to_s}')
GROUP BY date_trunc('day', created_at)
ORDER BY date_trunc('day', created_at)
)
SELECT date(filled_dates.day), coalesce(stats.count, filled_dates.blank_count) as count
FROM filled_dates
LEFT OUTER JOIN stats on stats.day = filled_dates.day
ORDER BY filled_dates.day;
Also: Postgresql uuid(), MD5(), etc.

Mais conteúdo relacionado

Destaque

House Location Photos
House Location PhotosHouse Location Photos
House Location Photos
kylelabrache
 
Session 41 Mathias Magnusson
Session 41 Mathias MagnussonSession 41 Mathias Magnusson
Session 41 Mathias Magnusson
mathmagn
 
UNICEF Russian digital landscape exploratory paper
UNICEF Russian digital landscape exploratory paperUNICEF Russian digital landscape exploratory paper
UNICEF Russian digital landscape exploratory paper
Akshay Sinha
 
วิทยาศาสตร์
วิทยาศาสตร์วิทยาศาสตร์
วิทยาศาสตร์
ikwanz
 

Destaque (15)

A message
A messageA message
A message
 
House Location Photos
House Location PhotosHouse Location Photos
House Location Photos
 
Session 41 Mathias Magnusson
Session 41 Mathias MagnussonSession 41 Mathias Magnusson
Session 41 Mathias Magnusson
 
UNICEF Russian digital landscape exploratory paper
UNICEF Russian digital landscape exploratory paperUNICEF Russian digital landscape exploratory paper
UNICEF Russian digital landscape exploratory paper
 
James shorty candies_1937_2011
James shorty candies_1937_2011James shorty candies_1937_2011
James shorty candies_1937_2011
 
Kyc
KycKyc
Kyc
 
Imagination with paper 'Filigree'
Imagination with paper 'Filigree'Imagination with paper 'Filigree'
Imagination with paper 'Filigree'
 
IWT subsidies - begeleiding door het innovatiecentrum
IWT subsidies - begeleiding door het innovatiecentrumIWT subsidies - begeleiding door het innovatiecentrum
IWT subsidies - begeleiding door het innovatiecentrum
 
Pata rat
Pata ratPata rat
Pata rat
 
1AOE UTA EL AMOR
1AOE UTA EL AMOR1AOE UTA EL AMOR
1AOE UTA EL AMOR
 
Bản tin số 8 Vào đời Khởi nghiệp
Bản tin số 8 Vào đời Khởi nghiệp Bản tin số 8 Vào đời Khởi nghiệp
Bản tin số 8 Vào đời Khởi nghiệp
 
会社を作ろうと思った時の思い
会社を作ろうと思った時の思い会社を作ろうと思った時の思い
会社を作ろうと思った時の思い
 
Prayer semminar
Prayer  semminarPrayer  semminar
Prayer semminar
 
วิทยาศาสตร์
วิทยาศาสตร์วิทยาศาสตร์
วิทยาศาสตร์
 
Willis human capital practice
Willis human capital practiceWillis human capital practice
Willis human capital practice
 

Semelhante a 10 Ruby and Rails Pro Tips

Semelhante a 10 Ruby and Rails Pro Tips (20)

Rest API Design Rules
Rest API Design RulesRest API Design Rules
Rest API Design Rules
 
Restful webservices
Restful webservicesRestful webservices
Restful webservices
 
My experience of Ruby Education in Taiwan
My experience of Ruby Education in TaiwanMy experience of Ruby Education in Taiwan
My experience of Ruby Education in Taiwan
 
Technical Introduction to YDN
Technical Introduction to YDNTechnical Introduction to YDN
Technical Introduction to YDN
 
Ruby on Rails Meets Enterprise Applications
Ruby on Rails Meets Enterprise ApplicationsRuby on Rails Meets Enterprise Applications
Ruby on Rails Meets Enterprise Applications
 
Java One Presentation - Ruby on Rails meets Enterprise
Java One Presentation - Ruby on Rails meets EnterpriseJava One Presentation - Ruby on Rails meets Enterprise
Java One Presentation - Ruby on Rails meets Enterprise
 
Intro to Rails and MVC
Intro to Rails and MVCIntro to Rails and MVC
Intro to Rails and MVC
 
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
 
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
 
Bridging the Gap: Single-Page Apps and AEM
Bridging the Gap: Single-Page Apps and AEMBridging the Gap: Single-Page Apps and AEM
Bridging the Gap: Single-Page Apps and AEM
 
2019 09 05 Global AI Night Toronto - Machine Learning.Net
2019 09 05 Global AI Night Toronto - Machine Learning.Net2019 09 05 Global AI Night Toronto - Machine Learning.Net
2019 09 05 Global AI Night Toronto - Machine Learning.Net
 
Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHP
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the Cloud
 
working with PHP & DB's
working with PHP & DB'sworking with PHP & DB's
working with PHP & DB's
 
Ruby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programingRuby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programing
 
Front End Development for Back End Java Developers - Jfokus 2020
Front End Development for Back End Java Developers - Jfokus 2020Front End Development for Back End Java Developers - Jfokus 2020
Front End Development for Back End Java Developers - Jfokus 2020
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Great APIs - Future of Your Progress App
Great APIs - Future of Your Progress AppGreat APIs - Future of Your Progress App
Great APIs - Future of Your Progress App
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API Changes
 
Os Minnee
Os MinneeOs Minnee
Os Minnee
 

Último

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Último (20)

Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 

10 Ruby and Rails Pro Tips

  • 1. 10 Ruby and Rails Pro Tips Michel Pigassou, Co-founder & CTO at Fidzup, michel@fidzup.com 2014-08-05 A few things you may or may not know
  • 2. Use debugger Just use it! How? http://guides.rubyonrails.org/debugging_rails_applications.html Bonus: byebug for Ruby 2: https://github.com/deivid-rodriguez/byebug
  • 3. Inline Assignments Don't: params[:routing_data] ? routing_data = params[:routing_data].to_json : routing_data = {} Do: routing_data = params[:routing_data] ? params[:routing_data].to_json : {}
  • 4. Fail Fast Integer() and Float() raise an Exception Also Hash#fetch Bonus: Array()
  • 5. Use Constants and Integers Example: CHOICES = [:female, :male, :unknown].freeze user.sex = CHOICES.index(:female) user.sex = CHOICES[0]
  • 6. Be Careful with =~ =~ returns an integer or nil ("foo" =~ /foo/) === true #=> false
  • 7. Eliminate Tautologies Don't: <%= radio_button_tag 'date_field', 'created_at', params[:date_field] == 'created_at' ? true : false %> Do: <%= radio_button_tag 'date_field', 'created_at', params[:date_field] == 'created_at' %>
  • 8. Use update_column update_attribute runs callbacks update_column doesn't
  • 9. Avoid nested_attributes Complicated with AJAX and deep nested associations Alternatives: - Do It Yourself - Redtape (https://github.com/ClearFit/redtape)
  • 10. Useful Gems Annotate (Schema in models) https://github.com/ctran/annotate_models Lograge (Better logs) https://github.com/roidrage/lograge factory_girl (Better fixtures) https://github.com/thoughtbot/factory_girl better_errors https://github.com/charliesome/better_errors
  • 11. Let the DB Do its Work Filling dates without value: WITH filled_dates as ( select day, 0 as blank_count from generate_series('#{@start_date}', current_date::date, '1 day') as day ), stats as ( SELECT COUNT(*) AS count, date_trunc('day', created_at) AS day FROM "checkins" WHERE (created_at >= '#{@start_date.to_s}') GROUP BY date_trunc('day', created_at) ORDER BY date_trunc('day', created_at) ) SELECT date(filled_dates.day), coalesce(stats.count, filled_dates.blank_count) as count FROM filled_dates LEFT OUTER JOIN stats on stats.day = filled_dates.day ORDER BY filled_dates.day; Also: Postgresql uuid(), MD5(), etc.