Emoticons in Rails Database

To support emoticons in a MySQL database you must use the utf8mb4 character set.
This isn’t a rails default.
To make sure the database uses the correct format I use the following migration to change the default charcter set

class InitDatabase < ActiveRecord::Migration[5.2]
  def up
    execute "ALTER DATABASE `#{connection.current_database}` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
  end
end

More info about the reason why I choose to use utf8mb4_unicode_ci as collection
https://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci/766996#766996

And force rails in using max string length of 191 which is required for the keylength of 767.
You can add the following monkey patch to the initializer

# config/initializers/monkey_patch_mysql_utf8mb4.rb
require 'active_record/connection_adapters/abstract_mysql_adapter'

module ActiveRecord
  module ConnectionAdapters
    class AbstractMysqlAdapter
      NATIVE_DATABASE_TYPES[:string] = { name: "varchar", limit: 191 }
    end
  end
end
Share

Rails Email Preview

Just a short post for myself to remember the ActionMailer Preview functionality.

test/mailers/previews/mailer_preview.rb

class MailerPreview < ActionMailer::Preview
  def mailer_method_name
    TeamPersonMailer.mailer_method_name( email_method_argument ) 
  end
end

And view your preview emails at: http://localhost:3000/rails/mailers

Share

Incompatible Rails Local Configuration

Today I experienced a problem when upgrading to rails 4.2 from rails 3.

When updating an Activerecord object with a DateTime value, I’ve got the following error:

NoMethodError: undefined method `getlocal' for Mon, 01 Jan -4712 00:00:00 +0000:DateTime

I had the following configuration in my Application object:

  config.active_record.default_timezone = :local  # store all values in the local time-zone 
  config.active_record.time_zone_aware_attributes = false # ignore the time zone

I don’t want time-zone information for this project.
But getting an error isn’t nice so I solved it by enabling the time_zone_aware_attributes.

  config.active_record.default_timezone = :local  # store all values in the local time-zone 
  config.active_record.time_zone_aware_attributes = true # ignore the time zone
Share