Rails Best Practices

19 votes

26498 views

remove trailing whitespace

Trailing whitespace always makes noises in version control system, it is meaningless. We should remove trailing whitespace to avoid annoying other team members.

implemented

Comments

flyerhzm

35 votes

35329 views

Use memoization

Memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs. In rails, you can easily use memoize which is inherited from ActiveSupport::Memoizable.

Comments

flyerhzm

5 votes

14294 views

Select specific fields for performance

In a system like forum, the title and body is displayed on show page, but only title is on index page. You should use select in query to speed up the query and save memory.

Comments

flyerhzm

13 votes

9569 views

Substituting before_filter :load_object

Instead of loading an object with a before_filter you can use a more intelligent helper_method to get the main object for the controller context.

Comments

by dbloete
dbloete

22 votes

19146 views

Use query attribute

Do you always check if ActiveRecord's attributes exist or not by nil?, blank? or present? ? Don't do that again, rails provides a cleaner way by query attribute

implemented

Comments

flyerhzm

11 votes

18859 views

Generate polymorphic url

If you want to generate different urls according to different objects, you should use the polymorphic_path/polymorphic_url to simplify the url generation.

Comments

flyerhzm

11 votes

4459 views

by Twols
Twols

1 votes

3378 views

Check if external gem-dependent classes are defined

If you have to set some external gem's config options in /config/enviroment.rb, contain it within `if defined?` block

Comments

by bartosz
bartosz

9 votes

10439 views

Use batched finder for large data query

If you want to do a large data query such as finding all the 10,000,000 users to send email to them, you should use batched finder to avoid eating too much memory.

Comments

flyerhzm

14 votes

24145 views

DRY bundler in capistrano

There are a few posts told you how to integrate bundler into capistrano, but they are out of date now. After bundler 1.0 released, you can add only one line in capistrano to use bundler.

implemented

Comments

flyerhzm