Archive for March, 2006

Table plurals and primary keys

March 31st, 2006 by papillon

This is how one redefines the table name and primary key column:

class MyTableName < ActiveRecord::Base
  set_table_name 'my_other_table_name'
  set_primary_key 'MyPrimaryKeyColumn'

Umlauts (special characters) and AJAX

March 25th, 2006 by papillon

There are two things that need to be done when you want to (or, in my case, have to) use an ISO character set throughout your application (i.e. database and web pages).

1) Set the character encoding to ISO-8859-1 in the HTTP header. From application.rb:

before_filter :set_charset
def set_charset
  @headers["Content-Type"] = "text/html; charset=ISO-8859-1"

(See “Foreign language” characters below)

2) Convert data sent via XMLHTTPREQUEST from UTF-8 to ISO-8859-1

Get the iconv library (instructions) (might already be included with Ruby 1.8.4).
Create a new file charset_conversion.rb in the helpers/ directory:

require 'iconv'
class Hash
  def iconv!(to,from)
    iconv =,from)

  def perform_iconv!(iconv)
    each_pair do |key,value|
      case value
        when String
          self[key] = iconv.iconv(value)
        when Hash

Include this file in application_helper.rb:

require 'charset_conversion'

In the action that receives the serialized XML data, add a call to iconv! before using the data:

def update
  params.iconv!("iso-8859-1", "utf-8")

This is essentially my interpretation of the notes from here: Ajax and Character Sets

Dates before 1970

March 20th, 2006 by papillon

The SQL Server adapter uses the Time class by default to represent datetime columns. This leads to problems when you want to represent dates before the year 1970: You would just get nil values.

I quickly found a similar solution (link lost) (scroll to 11/17/05) on the net and implemented the fix like this in my application_helper.rb file:

# Make the SQL Server Adapter work with dates before 1970
module ActiveRecord
  module ConnectionAdapters
    class ColumnWithIdentity
      def cast_to_time(value)
        return value if value.is_a?(Time)
        time_array = ParseDate.parsedate(value)
        time_array[0] ||= 2000
        time_array[1] ||= 1
        time_array[2] ||= 1
          Time.send(Base.default_timezone, *time_array)
[0], time_array[1],
          time_array[2], time_array[3],time_array[4],
          time_array[5]) rescue nil
    end #class ColumnWithIdentity
  end #module ConnectionAdapters
end #module ActiveRecord

This overwrites the standard function of the SQL Server Adapter and returns a DateTime if a normal Time is not enough for the given data.

NWNX4 Developer call

March 13th, 2006 by papillon

I posted the official announcement of the new Neverwinter Nights Extender for NWN2 today. It’s name will be NWNX4.

The main work will be on the program itself, but the website and the documentation section will get an overhaul as well.

To my pleasure, numerous people have already offered to help !

“Foreign language” characters

March 2nd, 2006 by papillon

The application I am working on is in german, so Rails needs to display Umlauts correctly (e.g. ä,ö,ü). At least with WebBrick, I was not able to convince it to send pages in ISO-8859-1, it always defaulted to UTF-8 which the Browsers would not interprete correctly.

My temporary solution is to overwrite the default codepage like this:

class ApplicationController < ActionController::Base
  before_filter :set_charset
  def set_charset
    @headers["Content-Type"] = "text/html; charset=ISO-8859-1"

Using that, the Umlauts appear correctly in all Browsers. Oh well, it works for now…

SQLServer and Paging

March 2nd, 2006 by papillon

Both the standard scaffold generator and my modified version of the AJAX scaffold support paging, i.e. displaying 10 items per page and allowing the user to flip through the pages with the usual first, last, next, previous links.

It does not work out of the box with SQL Server, unfortunately. You have to tell the driver explicitely that it should order the table by your primary key (id). Example:

@header_pages, @headers =
  paginate :headers, :per_page => 10,
  :order => ’id asc’

Rails and SQL Server

March 1st, 2006 by papillon

This page include almost everything you need to get started with SQL Server (2005): HowtoConnectToMicrosoftSQLServer

Basically, you have to copy the file ADO.rb from the latest Ruby-DBI source distribution to the directory


and then set up your database.yml like this:

adapter: sqlserver
database: mydb
host: myhost
username: myuser
password: mypassword