Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8981053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:14:16+00:00 2026-06-15T20:14:16+00:00

I need to connect to a legacy SQL Server 2000 database using their own

  • 0

I need to connect to a legacy SQL Server 2000 database using their own conventions and specially CamelCase columns and Tables.

For tables it seems fine, Rails is asking it with lowercase and the database find it nicely. The issue is with the columns because Rails fetch their name with SQL and thus get whatever case their name is.

I’m dealing with 500+ tables with some dozen columns in each of them and several legacy applications running in production above them so renaming the columns is no solution.
Using alias_attribute is also a way-too-much-work solution.

I don’t want to have some weird case in my code too like client.AccountId (just looks like Java code).

So my final question is: is there any way to have Rails dealing with lowercase methods and symbols which are then used in whatever-case the database uses when dealing with SQL ?
I’m looking for any existing solution or even a direction to the sensible area of ActiveRecord where all this mechanics is done (I’ve been searching but the source code is huge …)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:14:17+00:00Added an answer on June 15, 2026 at 8:14 pm

    OKay some time after posting the question I had a flash idea that alias_attribute was actually the solution but just needed a bit of magic over it.
    Here is the solution to my own problem:

    module LegacyDatabase
      module ClassMethods
        def aliased_attributes
          @aliased_attributes ||= {}
        end
    
        def alias_attribute(new_name, old_name)
          self.aliased_attributes[new_name.to_sym] = old_name.to_sym
          super(new_name, old_name)
        end
      end
    
      module InstanceMethods
        private
        def read_attribute(attr_name)
          attr_name = self.class.aliased_attributes[attr_name.to_sym] if self.class.aliased_attributes.has_key?(attr_name.to_sym)
          super(attr_name)
        end
    
        def write_attribute(attr_name, value)
          attr_name = self.class.aliased_attributes[attr_name.to_sym] if self.class.aliased_attributes.has_key?(attr_name.to_sym)
          super(attr_name, value)
        end
      end
    
      def self.included(base)
        base.instance_eval do
          extend(ClassMethods)
          include(InstanceMethods)
        end
    
        base.columns.each do |column|
          legacy_name = column.name
          rails_name  = column.name.underscore
          if legacy_name != rails_name
            base.alias_attribute rails_name, legacy_name
          end
        end
      end
    end
    

    I think this is the minimum code modification possible to avoid messing all ActiveRecord code. I’d like your opinion on this and your comments if you see a wall I’m going to hit and I don’t !

    To describe the solution, I’m using the columns method of ActiveRecord to generate snake_case looking aliases for each column. I’m also giving alias_column a memory of the aliases, that way read and write attribute methods know when they are dealing with alias names.

    Since in my legacy database the convention for the ID or the table Table is TableID, my solution will create a table_id alias found by ActiveRecord using the “table_name_with_underscore” convention, so the id method is working as expected.

    I presume it’s not going to work with all the SQL fetches, even with Squeel of something but I don’t think there is any simple solution for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to connect to an Oracle database server with system identifier PROD, using
I need to connect to a SQL server 2000 from SQL server 2008. but
I have an SQL database and want to connect to it using VBA in
I need to connect to a Sqlite database, I am using following code but
I'm developing an android application. I need connect to sql server. I wrote a
I need to connect to the middleware server using java URL and URLConnection classes
I need to connect to a website using a proxy server. I can do
I need connect to server git. http://help.github.com/win-set-up-git/ I do everything to the point, 4
i need to connect to another server we manage and have it's results (in
I need to connect to an external LDAP server that is accessible to me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.