The danger of Rails.env.production?
Updated version of an article first published on March 23rd, 2017.
When I do a code review, one of the scariest things I see is logic like this:
if Rails.env.production? do_additional_work end
Why? Your beautiful tests and tightly integrated CI system won't execute that code. You won't see that code execute as you refresh your browser in development. From syntax errors to
- if Rails.env.production? = render partial: 'shared/google_analytics'
<% if event=flash[:event] %> ga('send', 'event', '<%=event.category%>', '<%=event.action%>'); <% end %>
If we shorted
Use an alternate API key
<% api_key = Rails.env.production? ? "PROD API KEY" : "TEST API KEY" %> ... ga('create', '<%= api_key %>', 'auto'); ...
This ensures the full path will be executed and we won't pollute our production analytics data.
Ready to optimize your site?
Adding servers can be a band-aid for slow code. Scout APM helps you find and fix your inefficient and costly code. We automatically identify N+1 SQL calls, memory bloat, and other code-related issues so you can spend less time debugging and more time programming.
Ready to optimize your site? Sign up for a free trial.