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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:29:02+00:00 2026-05-18T23:29:02+00:00

Short version: In trying to deal with a legacy database in Ruby-on-Rails, I’ve come

  • 0

Short version: In trying to deal with a legacy database in Ruby-on-Rails, I’ve come across some class-loading behavior that I don’t understand.

Long version: At my job, I’m working on a PHP codebase running against a MySQL database with sometimes-helpful, but usually-frustrating naming conventions. So, it doesn’t fit into the typical Ruby on Rails naming patterns. An example:

create table objectOfSomeType (
    OOSTid int primary key auto_increment,
    OOSTatooID int,
    OOSTname varchar(255) not null,
    OOSTotherThing datetime,
    OOSTenteredDate timestamp not null,
    OOSTenteredBy varchar(32) not null,
    OOSTmodifiedDate timestamp,
    OOSTmodifiedBy varchar(32)
    /* etc. */
);

create table another_type_of_object (
    ATOOid int primary key auto_increment,
    ATOOname varchar(255) not null
    /* etc. */
);

Notable properties:

  • objectOfSomeType – table name is singular, sometimes camelCased, sometimes under_scored
  • OOST (Object Of Some Type) – columns all have a shared prefix
  • OOSTid – virtually all tables have an auto-increment primary key = prefix + ‘id’
  • OOSTatooID – indicates a foreign key relationship against the anotherTypeOfObject table (which has prefix = ‘ATOO’)
  • OOST{entered,modified}Date – correspond to RoR’s {created,updated}_at

I tried to set up a subclass of ActiveRecord::Base that had some functions for dealing with the table prefixes. E.g.:

class ActiveRecordJob < ActiveRecord::Base
    class_inheritable_accessor :table_prefix

    class << self
        # this class needs no table
        abstract_class = true

        # function to setup the prefix
        def set_table_prefix(prefix)
            self.table_prefix = prefix
            set_primary_key "#{prefix}id"
        end

        # override the standard column_names to return prefix-less names
        def column_names
            names = super
            if pfx = self.table_prefix
                # obviously not a Rubist...
                names = names.map { |n| n.sub(%r|^#{pfx}|, "") }
            end
            names
        end
    end

    # override method_missing to set up non-prefixed versions
    # if the prefixed version is found, otherwise fall back to super
    def method_missing(method, *args)
        if pfx = self.table_prefix
            prefixed = pfx + method
            if respond_to?(prefixed)
                self.class.send(:define_method, method.to_sym) do
                    return send(prefixed, *args)
                end
                return send(method, *args)
            end
        end
        super
    end
end

class ObjectOfSomeType < ActiveRecordJob
    set_table_name :objectOfSomeType
    set_table_prefix :OOST
    belongs_to :atoo, :class_name => 'AnotherTypeOfObject', :foreign_key => 'OOSTatooID'
end

class AnotherTypeOfObject < ActiveRecordJob
    set_table_name :anotherTypeOfObject
    set_table_prefix :ATOO
end

This works well enough for single models (an ObjectOfSomeType object has name and otherProperty attributes). But, something is going wrong with the associations. By way of example, in the object_of_some_type/show.html.erb template:

This works fine, no need for .OOSTname:
<%= h @oost.name %>

This works fine, accessing the prefixed name directly:
<%= h @oost.atoo.ATOOname %>

But this throws an exception, because the custom method_missing is not hit:
<%= h @oost.atoo.name %>

Surprisingly (to this Ruby noob), this does work.
<%= h @oost.atoo.class.find(@oost.atoo.id).name %> <!-- this line marked -->

After the above, working call, this also works:
<%= h @oost.atoo.name %>

What does the marked line do that “fixes” the problem? I’m likely going to go a different route (setting up a database full of RoR-named views against this database), but I’d still like to fill in this gap in my Ruby knowledge.

  • 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-18T23:29:03+00:00Added an answer on May 18, 2026 at 11:29 pm

    ActiveRecord is quite opinionated when it comes to naming conventions. You might have considerably more luck using DataMapper, which allows you to set up a model that uses its own Rails-like naming conventions for its attributes, but on save or update they are translated into their underlying column names in the database.

    http://datamapper.org/docs/legacy

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

Sidebar

Related Questions

Ladies & Gentlemen, Short Version: Trying to dynamically create a Spine Model Class. Basically
Short version: I'm trying to determine the best way to track what the user
Short version: I'm trying to figure out how to use the dsn argument in
To give the short version of the story here, I'm trying to make an
Short version of my questions: Can anyone point me toward some good, detailed sources
Short Version How can I do concatMap in MATLAB? I'm trying to build a
Super short version: I'm trying to use a user-defined function to populate a new
Short Version: Templating done completely with serving one html file and loading page-specific data
Short version: Can I grant access to external databases to a role? Long version:
Short version : I would like the maven-glassfish-plugin to only be executed in the

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.