Rails Best Practices

0 votes

958 views

Use Time.zone.now instead of Time.now

The ActiveSupport method Time.zone.now should be used in place of the Ruby method Time.now to pickup the local time zone.

Comments

by dankohn
dankohn

5 votes

15957 views

Don't modify the params hash

The params hash contains all the data that was submitted from a request. If you modify it, later code won't have access to it. Instead, copy the params hash and modify the copy.

Comments

daviddavis

18 votes

25779 views

default_scope is evil

activerecord provides default_scope to set a default scope for all operations on the model, it looks convenient at first, but will lead to some unexpected behaviors, we should avoid using it.

Comments

flyerhzm

12 votes

25247 views

Clever enums in rails

After many years of rails developing I have finally found satisfying solution to implement enums in rails.

Comments

codequest

4 votes

14960 views

monitor your backend services

We always have multiple processes for rails websites, if any of them crashed, your website failed, so it would be better to monitor all of the processes and automatically restart crashed processes.

Comments

flyerhzm

2 votes

13531 views

Pay more attentions on security

Recently we saw rails exposed some security issues, github was attacked, rubygems.org was crashed, they all remind us we must pay more attentions on our rails projects.

Comments

flyerhzm

8 votes

21438 views

speed up assets precompile with turbo-sprockets-rails3

Rails is integrated with sprockets from 3.1, which gives you the power to pre-process, compress and minify your assets. It's awesome, but it slows down deployment a lot.

implemented

Comments

flyerhzm

1 votes

20907 views

Check the return value of "save", otherwise use "save!"

The "save" method on ActiveRecord returns "false" and does nothing if the record is invalid. You should always check the return value, otherwise you may inadvertently not save the record. If you think the record can never be invalid, or don't want to check the return value, use "save!"

implemented

Comments

flyerhzm

10 votes

41090 views

Don't rescue Exception, rescue StandardError

In C# or Java, you can `catch (Exception)` to rescue all exception types. However, in Ruby you should almost never catch `Exception`, but only catch `StandardError`.

implemented

Comments

by ricbrad
ricbrad

0 votes

17522 views

Tell, don't ask

Methods should focus on what you want done and not how you want it.

Comments

by zamith
zamith