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
Use model association to avoid assigning reference in controller.implemented
You can use scope access to avoid checking the permission by comparing the owner of object with current_user in controller.implemented
Do not assign the model's attributes directly in controller. Add model virtual attribute to move the assignment to model.implemented
Use model callback can avoid writing some logic codes in controller before or after creating, updating and destroying a model.
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 methodimplemented
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
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.
Use accepts_nested_attributes_for to make nested model forms much easier, this feature is provided by rails 2.3
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
Some people will define 3 or more level nested routes, it's a kind of over design and not recommended.implemented
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
According to the decoupling principle, model should do finders by itself, a model should not know too much about associations finders logic.implemented
named_scope is awesome, it makes your codes much more readable, you can also combine named_scope finders to do complex finders.
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
If you find some methods whose definitions are similar, only different by the method name, it may use meta programming to simplify the things.
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.
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.
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
Rails 2.3.4 provides db:seed task that is the best way to insert seed data for set up a new application.implemented
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
Don't repeat yourself in controller, use before_filter to avoid duplicated codes.implemented
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
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
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
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
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