Fork me on GitHub

Move code into helper

24 Jul 2010

Wen-Tien Chang

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

Tags 


Move code into controller

24 Jul 2010

Wen-Tien Chang

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

Tags 


Isolating Seed Data

24 Jul 2010

Wen-Tien Chang

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

Tags 


Extract to composed class

24 Jul 2010

Wen-Tien Chang

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. Read More

Tags 


Extract into Module

24 Jul 2010

Wen-Tien Chang

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. Read More

Tags 


DRY Metaprogramming

24 Jul 2010

Wen-Tien Chang

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

Tags 


DRY Controller (debate)

24 Jul 2010

Wen-Tien Chang

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 Read More

Tags 


Always add DB index

24 Jul 2010

Wen-Tien Chang

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. Read More

Tags 


Love named_scope

23 Jul 2010

Wen-Tien Chang

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

Tags 


Keep Finders on Their Own Model

23 Jul 2010

Wen-Tien Chang

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

Tags