Share a Rails Tip Weekly!

rails

#41

In addition to rake tmp:clear, I also use rake log:clear when I want to zip up my repo and send it over email (old school I know)


#42

I have found recently that rake db:migrate will add the new migrations for both development environment and test environment but for a rollback it only undoes the development version. So to do both I do.

rake db:rollback STEP=1
RAILS_ENV=test rake db:rollback STEP=1

#43

Now I create a controller class per action
like

class Book::Create::ActionController
  def call

  end

  private

  def one_of_many_private_methods
  end
end

#44

Time.now is not timezone aware and uses server time. So prefer using Time.zone.now


#45

Use AR::Relation#exists instead of AR::Relation#count / #empty? for faster DB query
(#empty? use #count behind unless loaded)


#46

One “pattern”/trick I learned at a job that keeps my code clean - use a module method to generate paths to external libraries (e.g., Javascript, CSS)… (code simplified below)

module LibraryHelper
   def handlebars_javascript
     'https://#{cdn_path_to_library}'
   end
end

then in your application layout file:

<%= javascript_include_tag handlebars_javascript, :application %>