Rails Best Practices

34 votes

29761 views

Use Observer

Observer serves as a connection point between models and some other subsystem whose functionality is used by some of other classes, such as email notification. It is loose coupling in contract with model callback.

implemented

Comments

by ihower
ihower

3 votes

5724 views

Extract to composed class

If a model has some related columns, e.g. a user has an address_city and an address_street, you can extract these properties into a composed class.

Comments

by ihower
ihower

16 votes

11231 views

Extract into Module

Some codes in your model are related, they are take charge of the same things, such as logging and authorization. We can extract these codes into a module to reuse them.

Comments

by ihower
ihower

6 votes

12607 views

DRY: Metaprogramming

If you find some methods whose definitions are similar, only different by the method name, it may use meta programming to simplify the things.

Comments

by ihower
ihower

23 votes

26884 views

the Law of Demeter

According to the law of demeter, a model should only talk to its immediate association, don't talk to the association's association and association's property, it is a case of loose coupling.

implemented

Comments

by ihower
ihower

5 votes

6754 views

Love named_scope

named_scope is awesome, it makes your codes much more readable, you can also combine named_scope finders to do complex finders.

Comments

by ihower
ihower

7 votes

5775 views

Keep Finders on Their Own Model

According to the decoupling principle, model should do finders by itself, a model should not know too much about associations finders logic.

implemented

Comments

by ihower
ihower

9 votes

7076 views

Not use default route if you use RESTful design

If you use RESTful design, you should NOT use default route. It will cause a security problem. I explain at http://ihower.tw/blog/archives/3265 too.

implemented

Comments

by ihower
ihower

4 votes

12889 views

Needless deep nesting

Some people will define 3 or more level nested routes, it's a kind of over design and not recommended.

implemented

Comments

by ihower
ihower

4 votes

8187 views

Overuse route customizations

According to Roy Fielding’s doctoral thesis, we should use restful routes to represent the resource and its state. Use the default 9 actions without overusing route customizations.

implemented

Comments

by ihower
ihower