ActiveRecord with Legacy Database


#1

Hiya RoR gurus,

I work at a .NET shop, but I enjoy learning Ruby. So, I wanna
prototype some of our product’s screens using Rails (and hopefullly
AJAX). One thing I’m not sure about, since I’m a Rails noob, is
whether ActiveRecord will get along with our database…

So I have two questions:

  • Can I have ActiveRecord get along with our PascalCase naming
    conventions?

  • We ALWAYS have two fields as our primary key: Id and
    EffectiveYear, which is an integer containing a year and a version, ie
    200600000, 200600001, … This is a strict requirement, so can I get
    ActiveRecord to do this (easily)?

Thanks in advance,

ZachZ


#2

Hi, I’m not a guru, but I also work with legacy tables.

  • Can I have ActiveRecord get along with our PascalCase naming
    conventions?
    I think so. Mostly anything in rails can be overriden. If your tables’
    names are not underscored, you can set_table_name in the models to your
    PascalCase names. And you can always access your columns’ names through
    the model using the PascalCase without a hitch.
  • We ALWAYS have two fields as our primary key: Id and
    EffectiveYear, which is an integer containing a year and a version, ie
    200600000, 200600001, … This is a strict requirement, so can I get
    ActiveRecord to do this (easily)?
    My partial conclusion is that you cannot specify primary key with two
    columns in the current rails. My workaround is to create a view that
    combines the two keys into one key called id, and for any tables that
    FKs this id, I created a view with a FK alias of the name
    referencedtable_id just so I don’t need to do overriding in the models.
    Sinmce you’re just prototyping, this can be quick and effective.

good luck!


#3

I can try making few views like you said for a prototype, but we have
A LOT of tables, so that would not be optimal in the future! Hmm…
Maybe there’s some more ideas out there…?


#4

I can try making few views like you said for a prototype, but we have
A LOT of tables, so that would not be optimal in the future! Hmm…
Maybe there’s some more ideas out there…?

Once you’ve tried on a few views, you could extract some general trends
out
of your legacy tables and automate the process (write a tool which will
generate the rails models with the default overriden for instance).

hope this helps

Thibaut


#5

Once you’ve tried on a few views, you could extract some general trends
out of your legacy tables and automate the process (write a
tool which will generate the rails models with the default overriden for
instance).

edit: the tool in question could be ruby+erb, or CodeSmith if you want
to do
that in c# :slight_smile:

cheers

-T


#6

Not a bad idea… They all have the same two fields for primary key:
Id and EffectiveYear