flyerhzm

flyerhzm

Login flyerhzm

Website http://www.huangzhimin.com/

17 votes

10630 views

Use asset_host for production server

Use asset host for cookie-free domains for components, that make your components load faster.

Comments

flyerhzm

4 votes

17571 views

Use multipart/alternative as content_type of email

Rails uses plain/text as the default content_type for sending email, you should change it to multipart/alternative that email clients can display html formatted email if they support and display plain text email if they don't support html format.

implemented

Comments

flyerhzm

10 votes

8364 views

Use css sprite automatically

Using css sprite can reduce a large number of http requests, so it makes the web page loaded much faster. It it painful to composite a lot of images manually, do it automatically.

Comments

flyerhzm

13 votes

37505 views

Use STI and polymorphic model for multiple uploads

This is a flexible and reusable solution for multiple uploads, using STI model to save all the uploaded assets in one "assets" table and using polymorphic model to reuse "Asset" model in different uploadable models.

Comments

flyerhzm

0 votes

52515 views

Fetch current user in models

I don't remember how many times I need to fetch current user in models, such as audit log. Here is a flexible way to set the current user in and fetch the current user from User model.

Comments

flyerhzm

9 votes

12534 views

Put scripts at the bottom

Do you experience that your website renders slow due to loading a lot of javascripts, especially loading some third-party javascripts? Move script tags to the bottom of body can speed up the render of your website.

Comments

flyerhzm

14 votes

23890 views

DRY bundler in capistrano

There are a few posts told you how to integrate bundler into capistrano, but they are out of date now. After bundler 1.0 released, you can add only one line in capistrano to use bundler.

implemented

Comments

flyerhzm

9 votes

10077 views

Use batched finder for large data query

If you want to do a large data query such as finding all the 10,000,000 users to send email to them, you should use batched finder to avoid eating too much memory.

Comments

flyerhzm

11 votes

17979 views

Generate polymorphic url

If you want to generate different urls according to different objects, you should use the polymorphic_path/polymorphic_url to simplify the url generation.

Comments

flyerhzm

22 votes

18342 views

Use query attribute

Do you always check if ActiveRecord's attributes exist or not by nil?, blank? or present? ? Don't do that again, rails provides a cleaner way by query attribute

implemented

Comments

flyerhzm

5 votes

13911 views

Select specific fields for performance

In a system like forum, the title and body is displayed on show page, but only title is on index page. You should use select in query to speed up the query and save memory.

Comments

flyerhzm

34 votes

33818 views

Use memoization

Memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs. In rails, you can easily use memoize which is inherited from ActiveSupport::Memoizable.

Comments

flyerhzm

19 votes

25438 views

remove trailing whitespace

Trailing whitespace always makes noises in version control system, it is meaningless. We should remove trailing whitespace to avoid annoying other team members.

implemented

Comments

flyerhzm

15 votes

33467 views

Simplify render in views

render is one of the often used view helpers, we can pass object, collection or local variables. From rails 2.3, more simplified syntax for render are provided.

implemented

Comments

flyerhzm

9 votes

16550 views

Simplify render in controllers

Like the simplify render in views, from rails 2.3, we can also simplify render in controllers.

implemented

Comments

flyerhzm

1 votes

11913 views

comment your magic codes

Ruby/Rails provides a lot of magic codes, especially for metaprogramming, they are powerful, less codes to implement more functions, but they are not intuition, you should write good comment for your magic codes.

Comments

flyerhzm

14 votes

6199 views

defer expensive job

If you want to process something expensive as part of a web request, it will delay the response. If the job is not critical, it's wiser to move the expensive to a background queue and returns the response immediately.

Comments

flyerhzm

26 votes

26283 views

Annotate your models

Are you tired of going to schema.rb to find your table structures information? It would be better to list all the attributes of the model in the model itself.

Comments

flyerhzm

13 votes

13088 views

Remove empty helpers

If you use rails generator to create scaffolds or controllers, it will also create some helpers, most of the helpers are useless, just remove them.

implemented

Comments

flyerhzm

31 votes

20390 views

split route namespaces into different files

the routes will become complicated with the growth of your application, contain different namespaces, each with a lot of resources and custom routes, it would be better to split routes into different files according to the namespaces, which makes it easy to maintain the complicated routes.

Comments

flyerhzm

-1 votes

9736 views

Remove tab

Using tabs can mess up the spacing since some IDE's use 4 spaces for a tab, while others use 2, and some people don't use tabs at all, a mix of tabs and spaces causes things to not line up in most cases.

implemented

Comments

flyerhzm

3 votes

18364 views

Optimize db migration

rails migration provides a convenient way to alter database structure, you can easily add, change and drop column to a existing table, but when the data in existing table are huge, it will take a long time to alter existing table, you should try to merge/optimize the db alter sql statements.

Comments

flyerhzm

10 votes

20555 views

Use cells to abstract view widgets

Rails developers always pay more attentions on models and controllers refactoring, they don't take care about views modularization, that makes view codes most difficult to maintain. Here I recommend you to use cells gem to write more reuseable, testable and cacheable view codes.

Comments

flyerhzm

26 votes

27219 views

Not use time_ago_in_words

It's very common for a rails developer to use time_ago_in_words to display time like "5 minutes ago", but it's too expensive to calculate the time in server side, you should utilize client cpu to calculate the time ago.

implemented

Comments

flyerhzm

6 votes

23772 views

Protect mass assignment

Rails mass assignment feature is really useful, but it may be a security issue, it allows an attacker to set any models' attributes you may not expect. To avoid this, we should add attr_accessbile or attr_protected to all models.

implemented

Comments

flyerhzm

20 votes

27200 views

use after_commit

Most developers use AR callbacks after_create/after_update/after_destroy to generate background job, expire cache, etc., but they don't realize these callbacks are still wrapped in database transaction, they probably got unexpected errors on production servers.

Comments

flyerhzm

4 votes

12194 views

rolling out with feature flags

Sometimes you may face the situation that some features will be released, but you are not sure if it is friendly to end user, or if it will lead to performance issues, at that time you should use what we called "feature flags"

Comments

flyerhzm

7 votes

14920 views

split your cap tasks into different files

Your capistrano deploy.rb file might become complicated with the growth of your application, contain more and more cap tasks, it would be better to split these tasks into different files according to the functionalities, which makes it easy to maintain, and they are more likely to be reused in the future.

Comments

flyerhzm

1 votes

16921 views

Check the return value of "save", otherwise use "save!"

The "save" method on ActiveRecord returns "false" and does nothing if the record is invalid. You should always check the return value, otherwise you may inadvertently not save the record. If you think the record can never be invalid, or don't want to check the return value, use "save!"

implemented

Comments

flyerhzm

8 votes

18211 views

speed up assets precompile with turbo-sprockets-rails3

Rails is integrated with sprockets from 3.1, which gives you the power to pre-process, compress and minify your assets. It's awesome, but it slows down deployment a lot.

implemented

Comments

flyerhzm

2 votes

10558 views

Pay more attentions on security

Recently we saw rails exposed some security issues, github was attacked, rubygems.org was crashed, they all remind us we must pay more attentions on our rails projects.

Comments

flyerhzm

3 votes

11470 views

monitor your backend services

We always have multiple processes for rails websites, if any of them crashed, your website failed, so it would be better to monitor all of the processes and automatically restart crashed processes.

Comments

flyerhzm

15 votes

19399 views

default_scope is evil

activerecord provides default_scope to set a default scope for all operations on the model, it looks convenient at first, but will lead to some unexpected behaviors, we should avoid using it.

Comments

flyerhzm