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

PDF
Amplify your stack - Jsfoo pune 2012
PDF
Rails Best Practices
PDF
Building Web Interface On Rails
PDF
RESTful Rails2
PDF
Boost your productivity!: Productivity tips for rails developers - Lightning ...
PDF
Ruby 1.9
KEY
25 Real Life Tips In Ruby on Rails Development
PDF
那些 Functional Programming 教我的事
Amplify your stack - Jsfoo pune 2012
Rails Best Practices
Building Web Interface On Rails
RESTful Rails2
Boost your productivity!: Productivity tips for rails developers - Lightning ...
Ruby 1.9
25 Real Life Tips In Ruby on Rails Development
那些 Functional Programming 教我的事

Viewers also liked (15)

PDF
House Location Photos
PDF
Session 41 Mathias Magnusson
PDF
UNICEF Russian digital landscape exploratory paper
PPT
James shorty candies_1937_2011
DOCX
PPTX
Imagination with paper 'Filigree'
PPTX
IWT subsidies - begeleiding door het innovatiecentrum
PPTX
Pata rat
PPTX
PDF
Bản tin số 8 Vào đời Khởi nghiệp
PPTX
会社を作ろうと思った時の思い
PPT
Prayer semminar
PDF
วิทยาศาสตร์
PDF
Willis human capital practice
House Location Photos
Session 41 Mathias Magnusson
UNICEF Russian digital landscape exploratory paper
James shorty candies_1937_2011
Imagination with paper 'Filigree'
IWT subsidies - begeleiding door het innovatiecentrum
Pata rat
Bản tin số 8 Vào đời Khởi nghiệp
会社を作ろうと思った時の思い
Prayer semminar
วิทยาศาสตร์
Willis human capital practice
Ad

Similar to 10 Ruby and Rails Pro Tips (20)

PPTX
Rest API Design Rules
PPTX
Restful webservices
PDF
My experience of Ruby Education in Taiwan
PDF
Technical Introduction to YDN
PPT
Java One Presentation - Ruby on Rails meets Enterprise
PPT
Ruby on Rails Meets Enterprise Applications
PPTX
Intro to Rails and MVC
PDF
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
PDF
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
PPTX
Bridging the Gap: Single-Page Apps and AEM
PPTX
2019 09 05 Global AI Night Toronto - Machine Learning.Net
PPT
Rails Vs CakePHP
PDF
JRuby, Ruby, Rails and You on the Cloud
PPTX
working with PHP & DB's
PDF
Ruby on Rails 3.1: Let's bring the fun back into web programing
PDF
Front End Development for Back End Java Developers - Jfokus 2020
KEY
Supa fast Ruby + Rails
PDF
Great APIs - Future of Your Progress App
PDF
Drupal 8 - Core and API Changes
PDF
Os Minnee
Rest API Design Rules
Restful webservices
My experience of Ruby Education in Taiwan
Technical Introduction to YDN
Java One Presentation - Ruby on Rails meets Enterprise
Ruby on Rails Meets Enterprise Applications
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)
Bridging the Gap: Single-Page Apps and AEM
2019 09 05 Global AI Night Toronto - Machine Learning.Net
Rails Vs CakePHP
JRuby, Ruby, Rails and You on the Cloud
working with PHP & DB's
Ruby on Rails 3.1: Let's bring the fun back into web programing
Front End Development for Back End Java Developers - Jfokus 2020
Supa fast Ruby + Rails
Great APIs - Future of Your Progress App
Drupal 8 - Core and API Changes
Os Minnee
Ad

Recently uploaded (20)

PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
System and Network Administration Chapter 2
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
AI in Product Development-omnex systems
PDF
medical staffing services at VALiNTRY
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Design an Analysis of Algorithms I-SECS-1021-03
Design an Analysis of Algorithms II-SECS-1021-03
Odoo POS Development Services by CandidRoot Solutions
Understanding Forklifts - TECH EHS Solution
Upgrade and Innovation Strategies for SAP ERP Customers
System and Network Administration Chapter 2
CHAPTER 2 - PM Management and IT Context
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Nekopoi APK 2025 free lastest update
How to Choose the Right IT Partner for Your Business in Malaysia
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Operating system designcfffgfgggggggvggggggggg
Softaken Excel to vCard Converter Software.pdf
AI in Product Development-omnex systems
medical staffing services at VALiNTRY
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
wealthsignaloriginal-com-DS-text-... (1).pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf

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.