ihower

ihower

Login ihower

Website http://ihower.tw/blog/

14 votes

12224 views

Move finder to named_scope

Complex finders in controller make application hard to maintain. Move them into the model as named_scope can make the controller simple and the complex find logics are all in models.

implemented

Comments

by ihower
ihower

13 votes

20935 views

Use model association

Use model association to avoid assigning reference in controller.

implemented

Comments

by ihower
ihower

18 votes

13062 views

Use scope access

You can use scope access to avoid checking the permission by comparing the owner of object with current_user in controller.

implemented

Comments

by ihower
ihower

8 votes

24403 views

Add model virtual attribute

Do not assign the model's attributes directly in controller. Add model virtual attribute to move the assignment to model.

implemented

Comments

by ihower
ihower

3 votes

8627 views

Use model callback

Use model callback can avoid writing some logic codes in controller before or after creating, updating and destroying a model.

Comments

by ihower
ihower

9 votes

10537 views

Replace Complex Creation with Factory Method

Sometimes you will build a complex model with params, current_user and other logics in controller, but it makes your controller too big, you should move them into model with a factory method

implemented

Comments

by ihower
ihower

3 votes

16325 views

Move Model Logic into the Model

In MVC model, controller should be simple, the business logic is model's responsibility. So we should move logic from controller into the model.

implemented

Comments

by ihower
ihower

3 votes

6931 views

model.collection_model_ids (many-to-many)

When you want to associate a model to many association models by checkbox on view, you should take advantage of model.collection_model_ids to reduce the code in controller.

Comments

by ihower
ihower

6 votes

14607 views

Nested Model Forms

Use accepts_nested_attributes_for to make nested model forms much easier, this feature is provided by rails 2.3

Comments

by ihower
ihower

4 votes

7889 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

4 votes

12303 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

9 votes

6831 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

7 votes

5550 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

5 votes

6543 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

22 votes

25460 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

6 votes

12372 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

16 votes

10870 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

3 votes

5584 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

34 votes

28752 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

11 votes

17123 views

Isolating Seed Data

Rails 2.3.4 provides db:seed task that is the best way to insert seed data for set up a new application.

implemented

Comments

by ihower
ihower

16 votes

30179 views

Always add DB index

Always add index for foreign key, columns that need to be sorted, lookup fields and columns that are used in a GROUP BY. This can improve the performance for sql query. If you're not sure which column need to index , I recommend to use http://github.com/eladmeidar/rails_indexes, which provide rake tasks to find missing indexes.

implemented

Comments

by ihower
ihower

-7 votes

116099 views

Use before_filter

Don't repeat yourself in controller, use before_filter to avoid duplicated codes.

implemented

Comments

by ihower
ihower

3 votes

6885 views

DRY Controller (and debate)

For CRUD resources, we always write the same 7 actions with duplicated codes. To avoid this, you can use inherited_resources plugin. But be careful, there is DRY controller debate!! (http://www.binarylogic.com/2009/10/06/discontinuing-resourcelogic/) 1. You lose intent and readability 2. Deviating from standards makes it harder to work with other programmers 3. Upgrading rails trouble

Comments

by ihower
ihower

10 votes

8520 views

Move code into controller

According to MVC architecture, there should not be logic codes in view, in this practice, I will introduce you to move codes into controller.

implemented

Comments

by ihower
ihower

10 votes

9598 views

Move code into model

According to MVC architecture, there should not be logic codes in view, in this practice, I will introduce you to move codes into model.

implemented

Comments

by ihower
ihower

11 votes

9132 views

Move code into helper

According to MVC architecture, there should not be logic codes in view, in this practice, I will introduce you to move codes into helper.

implemented

Comments

by ihower
ihower

25 votes

33509 views

Replace instance variable with local variable

In partial view, we can use the instance variable directly, but it may be confused and make it hard to reuse anywhere, because we don't know exactly which instance variable can be used, so use the local variable in partial with explicitly assignment.

implemented

Comments

by ihower
ihower

29 votes

19691 views

Fix N+1 Queries

N+1 Queries is a serious database performance problem. Be careful of that situation! If you're not sure, I recommend you install http://github.com/flyerhzm/bullet plugin, which helps you reduce the number of queries with alerts (and growl).

Comments

by ihower
ihower