Rails Best Practices

3 votes

6691 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

3 votes

15491 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

9 votes

10020 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

8356 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

8 votes

23378 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

18 votes

12033 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

13 votes

20181 views

Use model association

Use model association to avoid assigning reference in controller.

implemented

Comments

by ihower
ihower

14 votes

11795 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