Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

WU Jun's Profile

GitHub User: quark-zju

Site: http://lihdd.net

Comments by WU Jun

Avatar

This is easy to understand:

  • foo package provides binary executable of foo.

  • foo-dev package contains header(.h) and library(.so) files for compiling related sources.

Since pg gem is a native gem that needs to be compiled against pg library, you need libpq-dev.

Avatar

And I want to point out a PGCon2009 video: PostgreSQL as a secret weapon for high-performance Ruby on Rails applications.

It suggests replace complex ActiveRecord logic with SQL to gain performance boost. The examples such as act_as_tree are impressing. The major disadvantage is that you can no more change db adapter easily.

Avatar

A simple note:

pg gem uses prepare statement in production env by default, which is different from MySQL.

This can be an issue if you are using unicorn. You may got PGError: ERROR: prepared statement "a3" already exists after several requests.

To solve this, make sure each unicorn process uses its own database connection by adding this to unicorn config file:

ruby
after_fork do |server, worker|
  ActiveRecord::Base.establish_connection
end

Hope this helps :)

Avatar

I wrote a colorful logger class.

It uses term-ansicolor gem and intends to be used in multi-process environment where pid is colored to make it easier to distinguish processes.

You can put that file in lib/ and use colorful logger directly like:

ruby
# logrotate: 4M * 8
config.logger = ColorfulLogger.new("log/#{Rails.env}.log", 8, 4 * 1024 ** 2)

or just pick some code and patch SimpleLogger.

Avatar

Asset Pipeline can be a little annoying if additional js/css other than application.{js,css} is required.

For example, with active_admin enabled, config/environments/production.rb needs a little change:

ruby
config.assets.precompile += %w( active_admin.js active_admin.css )

I would like to have this precompile array automatically set, but no good solution yet.

Avatar

Timezone is not easy as it looks. Timezone-related code must be carefully written.

I tried to find an easy way to set Rails config.time_zone to local but failed.

Now I use an initializer:

ruby
# from rake task time:zones:local
jan_offset = Time.now.beginning_of_year.utc_offset
jul_offset = Time.now.beginning_of_year.change(:month => 7).utc_offset
offset = jan_offset < jul_offset ? jan_offset : jul_offset

offset = if offset.to_s.match(/(\+|-)?(\d+):(\d+)/)
           sign = $1 == '-' ? -1 : 1
           hours, minutes = $2.to_f, $3.to_f
           ((hours * 3600) + (minutes.to_f * 60)) * sign
         elsif offset.to_f.abs <= 13
           offset.to_f * 3600
         else
           offset.to_f
         end

ActiveSupport::TimeZone.all.each do |zone|
  if offset.nil? || offset == zone.utc_offset
    # use that timezone
    Rails.application.config.time_zone = zone.name
    break
  end
end