SlideShare a Scribd company logo
1 of 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.

More Related Content

Viewers also liked

House Location Photos
House Location PhotosHouse Location Photos
House Location Photoskylelabrache
 
Session 41 Mathias Magnusson
Session 41 Mathias MagnussonSession 41 Mathias Magnusson
Session 41 Mathias Magnussonmathmagn
 
UNICEF Russian digital landscape exploratory paper
UNICEF Russian digital landscape exploratory paperUNICEF Russian digital landscape exploratory paper
UNICEF Russian digital landscape exploratory paperAkshay Sinha
 
James shorty candies_1937_2011
James shorty candies_1937_2011James shorty candies_1937_2011
James shorty candies_1937_2011Deena Chadwick
 
Imagination with paper 'Filigree'
Imagination with paper 'Filigree'Imagination with paper 'Filigree'
Imagination with paper 'Filigree'Makala (D)
 
IWT subsidies - begeleiding door het innovatiecentrum
IWT subsidies - begeleiding door het innovatiecentrumIWT subsidies - begeleiding door het innovatiecentrum
IWT subsidies - begeleiding door het innovatiecentruminnovatiecentra
 
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 Hoa Sen University
 
会社を作ろうと思った時の思い
会社を作ろうと思った時の思い会社を作ろうと思った時の思い
会社を作ろうと思った時の思いYasunori Hirasaka
 
วิทยาศาสตร์
วิทยาศาสตร์วิทยาศาสตร์
วิทยาศาสตร์ikwanz
 
Willis human capital practice
Willis human capital practiceWillis human capital practice
Willis human capital practicebobfarnham1
 

Viewers also liked (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
 

Similar to 10 Ruby and Rails Pro Tips

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 TaiwanMu-Fan Teng
 
Ruby on Rails Meets Enterprise Applications
Ruby on Rails Meets Enterprise ApplicationsRuby on Rails Meets Enterprise Applications
Ruby on Rails Meets Enterprise Applicationsdan_mcweeney
 
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 Enterprisedan_mcweeney
 
Intro to Rails and MVC
Intro to Rails and MVCIntro to Rails and MVC
Intro to Rails and MVCSarah Allen
 
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)Pixie Labs
 
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)Zain Asgar
 
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 AEMrbl002
 
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.NetBruno Capuano
 
Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHPGautam Rege
 
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 CloudHiro Asari
 
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 programingBozhidar Batsov
 
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 2020Matt Raible
 
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 AppGabriel Lucaciu
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesShabir Ahmad
 

Similar to 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
 

Recently uploaded

%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 masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
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 studentsHimanshiGarg82
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
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...SelfMade bd
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 

Recently uploaded (20)

%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
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
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
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
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...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 

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.