Archive for November, 2005

ActiveRecord Model & Legacy Table Column Names

I’m using a legacy database that has tables with column names that are UPPER\CASE. What I’d like is to have the UPPER\_CASE column names inserted as lower\_case attribute keys so that I can use _model.some\_attribute instead of model.SOME\_ATTRIBUTE. Instead, I found that I could override method\_missing in ActiveRecord::Base to check for the UPPER\CASE version of _some\_attribute. Since the method is private, I had to copy the whole thing. I put it into a base class that my models inherit from. All the modification does is check if the method\_name.upcase is in the attributes hash. If so, it upcases the method\_name and uses that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class AbstractLegacyModel < ActiveRecord::Base
  def method_missing(method_id, *args, &block)
    method_name = method_id.to_s
    # the next line is new code:
    method_name.upcase! if @attributes.include?(method_name.upcase)
    if @attributes.include?(method_name)
      define_read_methods if self.class.read_methods.empty? && self.class.generate_read_methods
      read_attribute(method_name)
    elsif self.class.primary_key.to_s == method_name
      id
    elsif md = /(=|\?|_before_type_cast)$/.match(method_name)
      attribute_name, method_type = md.pre_match, md.to_s
      # next line is new code
      attribute_name.upcase! if @attributes.include?(attribute_name.upcase)
      if @attributes.include?(attribute_name)
        case method_type
          when '='
            write_attribute(attribute_name, args.first)
          when '?'
            query_attribute(attribute_name)
          when '_before_type_cast'
            read_attribute_before_type_cast(attribute_name)
        end
      else
        super
      end
    else
      super
    end
  end
end

I’m not sure if this will work across the board, so I’ve got to write some tests to cover this.

It would be great if rails would automatically downcase table column names, so I put in a feature request for that today.

RSS for Blogs was Broken.

I discovered today after several days on the head version of Typo that my RSS feeds were broken. It turns out that Typo’s migration script missed adding the article_id column of the resources table. Once I added that, the feeds started working again. I marked it at http://typo.leetsoft.com/trac/ticket/552.

Using Active Record to Test a SQL Server Legacy DB Connection

I recently came across Sam Ruby’s Rails Confidence Builder post and used it as a model to test out my connection to a legacy SQL Server database. I’m undertaking a new project with Mel at Upstate and wanted to verify connectivity before diving into the Rails application. I’m using Actual Technologies SQL Server Driver because it’s easy to set up and use, making it worth the 30 bucks. One thing I noticed right away was that the attribute names come back in the same case as they are in the table definitions, so I’ll have to translate those to lower case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require 'rubygems'
require_gem 'activerecord'

ActiveRecord::Base.establish_connection(
  :adapter  => "sqlserver",
  :mode =>  "odbc",
  :dsn => "the_dsn",
  :database =>  "db_name",
  :username =>  "userName",
  :password =>  "some_password"
)  

class Customer < ActiveRecord::Base
  set_primary_key "customer_id"
  set_table_name "customer"
end

Customer.find(:all).each do |customer|
  puts customer.NAME
end

Another cool thing is that in TextMate all I have to do is plop the code in a new buffer and press CMD-R to run it.

Dang Development’s New Theme

This is actually a temporary theme. I had the header area developed a while back and decided that I’d rather use it for my personal site, but until I get a new one for Dang Development I like it a lot better than Azure and Laughing At You. I spent quite a bit of time digging around in Typo today and created a patch for some new div tags and css styles. It seemed to be missing a wrapper for comments, and had some hard-coded styling embedded in it. I’m sure I’ll tweak this a bit, for example the green links are a bit dim, but overall, I’m pleased with it.

Ruby is the Future of Java

Mel and I have been joking about Ruby and Java lately. I keep half-joking that “Ruby is the future of Java”, while he has a variation on the theme. For years now we Java developers have been coming up with numerous ways to make Java act like a dynamic language. Why not just use one?


Pages

Categories

Copyright ©2008-2013 James E Orchard-Hays, All Rights Reserved