ihower
Login ihower
Website http://ihower.tw/blog/
10 votes
2 comments
4709 views
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.
implemented8 votes
7 comments
6692 views
16 votes
4 comments
4841 views
You can use scope access to avoid checking the permission by comparing the owner of object with current_user in controller.
implemented5 votes
0 comments
8584 views
Do not assign the model's attributes directly in controller. Add model virtual attribute to move the assignment to model.
implemented3 votes
0 comments
2895 views
Use model callback can avoid writing some logic codes in controller before or after creating, updating and destroying a model.
7 votes
0 comments
3226 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
implemented3 votes
3 comments
4790 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.
implemented3 votes
0 comments
2500 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.
6 votes
2 comments
4524 views
Use accepts_nested_attributes_for to make nested model forms much easier, this feature is provided by rails 2.3
4 votes
0 comments
2821 views
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.
implemented5 votes
2 comments
3777 views
Some people will define 3 or more level nested routes, it's a kind of over design and not recommended.
implemented9 votes
0 comments
3233 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.
implemented5 votes
3 comments
2359 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.
implemented5 votes
3 comments
2318 views
named_scope is awesome, it makes your codes much more readable, you can also combine named_scope finders to do complex finders.
15 votes
10 comments
6812 views
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.
implemented5 votes
5 comments
2863 views
If you find some methods whose definitions are similar, only different by the method name, it may use meta programming to simplify the things.
13 votes
1 comments
2980 views
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.
3 votes
3 comments
2442 views
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.
26 votes
7 comments
9782 views
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.
implemented10 votes
8 comments
6940 views
Rails 2.3.4 provides db:seed task that is the best way to insert seed data for set up a new application.
implemented13 votes
12 comments
10415 views
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-7 votes
24 comments
28609 views
Don't repeat yourself in controller, use before_filter to avoid duplicated codes.
implemented3 votes
5 comments
3009 views
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
10 votes
0 comments
3482 views
According to MVC architecture, there should not be logic codes in view, in this practice, I will introduce you to move codes into controller.
implemented10 votes
8 comments
4035 views
According to MVC architecture, there should not be logic codes in view, in this practice, I will introduce you to move codes into model.
implemented9 votes
2 comments
3889 views
According to MVC architecture, there should not be logic codes in view, in this practice, I will introduce you to move codes into helper.
implemented19 votes
9 comments
9281 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.
implemented23 votes
8 comments
7398 views
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).
