29 Jun 2015

Richard Huang

TLDR, eager_group fixes n+1 aggregate sql functions for rails.

After consulting on some rails projects, I find most projects are still suffering from N+1 query problem. Many rails developers aren't aware that they generate hundreds sqls in one request, it's horrible.

What's n+1 query problem?

It occurs when you fetch n records from database and send additional sql for each record, let me give you an example, we have Post and Comment models and one Post has many Comments.

class Post < ActiveRecord::Base
  has_many :comments

class Comment < ActiveRecord:Base
Read More


solutions to cors problem

03 Feb 2015

Richard Huang

It's more and more popular that engineers will use different subdomains to build a website, like www for webapp, api for api server, assets for asset host, then you probably face the cors problem.

It can be solved either on app server or web server.

On app server (rails server), you can just add rack-cors gem to fix it.

On web server (apache or nginx), it can also be fixed by server config, the following is a nginx config to fix font cors issue for asset host.

server {
    server_name assets.xinminlabs...
Read More


New rails-bestpractices.com on github

14 Jan 2015

Richard Huang

We built rails-bestpractices.com 4 years ago, many rails developers come, discuss and share, we really appreciate it.

rails-bestpractices.com was built by rails, old code is on github, old website askd developer sign in first, then he can share post, vote to like or dislike a post, ask or answer a question, etc.

It works good, but we look a bit arbitrary, when developer shares his post, it ...

Read More


Asset Pipeline in ActionMailer on a different host

05 Jan 2015

Richard Huang

We have a rails project which sends email in sidekiq job, it's working well until it's deployed to production server, we found images in email are all not found.

The problem is we have 2 servers on production, app and db, we precompile assets on app server but run sidekiq on db server, then we call image_tag in mailer view, it generates image path as /assets/user.png but app server doesn't recognize such asset path, it expects image path as /assets/user-b1dbf9eed0aec90667f9589d7e0264c3.png.

Why ...

Read More