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 294061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:19:58+00:00 2026-05-12T06:19:58+00:00

Im working on a legacy oracle database with a slightly odd table naming convention

  • 0

Im working on a legacy oracle database with a slightly odd table naming convention where each column name is prefixed with the tables initial letters – eg policy.poli_id.

To make this database easier to work with I have a method set_column_prefix that creates accessors for each column with the prefix removed. ie:

# Taken from wiki.rubyonrails.org/rails/pages/howtouselegacyschemas
class << ActiveRecord::Base
  def set_column_prefix(prefix)
    column_names.each do |name|
      next if name == primary_key

      if name[/#{prefix}(.*)/e]
        a = $1

        define_method(a.to_sym) do
          read_attribute(name)
        end

        define_method("#{a}=".to_sym) do |value|
          write_attribute(name, value)
        end

        define_method("#{a}?".to_sym) do
          self.send("#{name}?".to_sym)
        end

      end
    end
  end
end

This is in a file (insoft.rb) in my lib/ directory, and required from from my config/environment.rb after the Rails::Initializer.run block.

This has been working fine in development, but when I try to run the application in production mode, I get the following error in all of my models:

dgs@dgs-laptop:~/code/voyager$ RAILS_ENV=production script/server 
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1964:in `method_missing': 
undefined method `set_column_prefix' for #<Class:0xb3fb81d8> (NoMethodError)
    from /home/dgs/code/voyager/app/models/agent.rb:16

This error is triggered by the ‘config.cache_classes = true’ line in config/environments/production.rb.
If I set this to false, then rails will start up, but won’t be caching classes. I’m guessing this makes rails cache all the models before it runs the Initializer block

If I move the ‘require “insoft.rb'” to before the start of the Rails::Initializer.run block, then I get errors because ActiveRecord hasn’t been initialized yet:

usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:443:in `load_missing_constant': uninitialized constant ActiveRecord (NameError)
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:80:in `const_missing'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:92:in `const_missing'
    from /home/dgs/code/voyager/lib/insoft.rb:1

Where should I be including this custom lib and set_column_prefix method in order for it to be picked up before the models are cached, but after all the activerecord files have loaded?

Cheers

Dave Smylie

  • 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-05-12T06:19:58+00:00Added an answer on May 12, 2026 at 6:19 am

    Where should I be including this custom lib and set_column_prefix method in order for it to be picked up before the models are cached, but after all the activerecord files have loaded?

    Try setting up an initializer. You can call it config/initializers/insoft.rb with the contents of your monkey patch:

    class << ActiveRecord::Base
      def set_column_prefix(prefix)
        column_names.each do |name|
          next if name == primary_key
    
          if name[/#{prefix}(.*)/e]
            a = $1
    
            define_method(a.to_sym) do
              read_attribute(name)
            end
    
            define_method("#{a}=".to_sym) do |value|
              write_attribute(name, value)
            end
    
            define_method("#{a}?".to_sym) do
              self.send("#{name}?".to_sym)
            end
    
          end
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a legacy database in Oracle and some of my tables have
I am a .Net web developer working with a legacy Oracle database. In the
Im working on a legacy database, that cant be changed. I have a table
i am working on a legacy oracle database that uses character literals T and
I am working with an oracle legacy database. All the dates are saved in
I am working with an Oracle legacy database and I am trying to map
I'm working with a legacy database whose keys follow a convention, but not the
I'm working with a legacy database that stores a boolean column as a string,
I'm currently working on a legacy system using Oracle's ADF Faces JSF implementation for
I'm working with a legacy Java app that's pulling data from Oracle. One of

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.