10 Ruby and Rails Pro Tips

Michel Pigassou
Michel PigassouSoftware Engineer at Zendesk
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.
1 of 11

More Related Content

Viewers also liked(15)

A messageA message
A message
Aulia Dindud Rachmawati219 views
House Location PhotosHouse Location Photos
House Location Photos
kylelabrache175 views
Session 41 Mathias MagnussonSession 41 Mathias Magnusson
Session 41 Mathias Magnusson
mathmagn255 views
James shorty candies_1937_2011James shorty candies_1937_2011
James shorty candies_1937_2011
Deena Chadwick413 views
KycKyc
Kyc
Saket Anand1.1K views
Imagination with paper 'Filigree'Imagination with paper 'Filigree'
Imagination with paper 'Filigree'
Makala (D)1.1K views
Pata ratPata rat
Pata rat
Chiorean Andrei1.1K views
1AOE UTA EL AMOR1AOE UTA EL AMOR
1AOE UTA EL AMOR
universidad tecnica de ambato144 views
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 University1.9K views
会社を作ろうと思った時の思い会社を作ろうと思った時の思い
会社を作ろうと思った時の思い
Yasunori Hirasaka1.1K views
Prayer  semminarPrayer  semminar
Prayer semminar
Roger Hernandez320 views
Willis human capital practiceWillis human capital practice
Willis human capital practice
bobfarnham1504 views

Similar to 10 Ruby and Rails Pro Tips(20)

Recently uploaded(20)

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.