Share a Rails Tip Weekly!

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)

1 Like

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
1 Like

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
1 Like

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

3 Likes

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

1 Like

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 %>

1 Like