The Rails Way: Jamis Buck and Michael Kosiarski
Goal is to describe the code style of rails through some examples.
Common anti pattern is a fat controller. Difficult to test. Common newbie mistake is to equate model with DB table. A model can be anything that encapsulates your data. It doesn’t have to have anything to do with the DB. One benefit of splitting things out are that you can now easily test independently from your controller. Also you can now use the form builder helpers that rails provides.
with_scope antipattern
before_create hook naming
Don’t depend on naming hook methods before_create etc. that is a readability issue. give the method a meaningful name and then use the before_create class method to hook it up.
Association proxies
Code at the level of thought, not DB. example: @ john.documents
instead of @ Document.find_all_by_user_id(john.id)
Apart from the readability benefit there are performance gain because subsequent calls in the same request won’t have to hit the DB.
Routes file
Use map.with_options to reduce documentation.
More Articles on Software & Product Development
- Agile With a Lowercase “a”
- ”Agile“ is an adjective. It is not a noun. It isn’t something you do, it is something you are.
- How Do You End Up With A Great Product A Year From Now?
- Nail the next two weeks. 26 times in a row.
- Build it Twice
- Resist the urge to abstract until you've learned what is general to a class of problems and what is specific to each problem.