Rails Best Practices

15 votes

35343 views

Simplify render in views

render is one of the often used view helpers, we can pass object, collection or local variables. From rails 2.3, more simplified syntax for render are provided.

implemented

Comments

flyerhzm

19 votes

27009 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

36112 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

14453 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

9707 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

19490 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

19160 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

4542 views

by Twols
Twols

1 votes

3413 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

10591 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