# Integrating with Legacy Databases

**URL:** <https://www.ruby-forum.com/t/integrating-with-legacy-databases/58363>\
**Category:** Rails\
**Created:** [April 20, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/integrating-with-legacy-databases/58363 "2006-04-20T22:26:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [April 20, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/integrating-with-legacy-databases/58363/1 "2006-04-20T22:26:29Z")

</div>

This seems like it must have been asked before - I really _did_ try to  
find it in the archives, so my apologies if it’s already out there.

Utilizing ActiveRecord, I would like to specify a prefix for the  
column names in my table. For example, in ‘Recipe 16 Integrating with  
Legacy Databases’ (Rails Recipes, from PragProg, by Fowler) they deal  
with integration with a WordPress db where all field names are  
preceded by “comment\_” – i.e. comment\_author\_email.

The recipe explains how to deal with the primary key (comment\_id) via:  
ActiveRecord::Base.primary\_key\_prefix\_type = :table\_name\_with\_underscore

which is great: the primary key would obviously be the most important  
piece to get working 🙂

However, it stops short of going into detail on dealing with this on  
the other column names.

I guess what I’m looking for is a nice clean way to take their extent  
their example - allowing me to reference the non-key fields as  
“author\_email” instead of needing to always prefix and say things like  
“comment\_author\_email” all over my whole application. and since this  
will be in a more complete environment: I also don’t want to remove  
“comment\_” from the front of every field name in the whole db! It  
will need to be specific to the Comment class itself.

Any help is most appreciated. Thanks.

- jim

---

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [April 20, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/integrating-with-legacy-databases/58363/2 "2006-04-20T22:57:17Z")

</div>

Hello Jim,

> However, it stops short of going into detail on dealing with this on  
> the other column names.
> 
> I guess what I’m looking for is a nice clean way to take their extent  
> their example - allowing me to reference the non-key fields as  
> “author\_email” instead of needing to always prefix and say things like  
> “comment\_author\_email” all over my whole application. and since this  
> will be in a more complete environment: I also don’t want to remove  
> “comment\_” from the front of every field name in the whole db! It  
> will need to be specific to the Comment class itself.

If I understand your problem correctly, let’s say we’ve got a Author  
model  
with a ‘comment\_author\_email’ field in the associated table, and  
you want to do things like :

a = Author.new  
a.author\_email = ‘…’

So you can use facade column ; in your model

class Author \< AR::B  
def author\_email  
read\_attribute(“comment\_author\_email”)  
end

def author\_email=(email)  
write\_attribute(“comment\_author\_email”, email.to\_s)  
end  
end

and do the same for the other fields. Maybe a sort of model scaffolding  
will ease your job 🙂 (one gotcha is the typecasting in the  
write\_attribute  
call)

My 2 (euro)cents,

– Jean-FranÃ§ois.

---

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [April 20, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/integrating-with-legacy-databases/58363/3 "2006-04-20T23:31:12Z")

</div>

Jean-François - Thanks for the help. Your example is just what I am  
trying to do. However, I am now wondering if maybe I don’t have my  
hands around exactly what is causing my issue…

For my exact application:  
my field name is “user\_email”, which I would like to refer to as  
“email”. My table name is “common\_users” and I have already taken  
care of it so that I can name my class “user”.

I have the code (as you have detailed below) in my user.rb:  
def email  
read\_attribute “user\_email”  
end  
def email=(value)  
write\_attribute “user\_email”, value.to\_s  
end

however I run into the following method and see an error when  
executing save (console output below the code)

def signup  
return if generate\_blank  
@params[‘user’].delete(‘form’)  
@user = User.new(@params[‘user’])  
begin  
User.transaction(@user) do  
@user.new\_password = true  
logger.warn “4444444444444444444444444444”  
if @user.save  
logger.warn “55555555555555555555555555555555”  
#snip  
end  
logger.warn “66666666666666666666666666”  
end  
logger.warn “777777777777777777”  
rescue  
logger.warn “888888888888888888888”  
flash.now[‘message’] = l(:user\_confirmation\_email\_error)  
end  
end

###############  
console output::  
User Columns (0.000580) SHOW FIELDS FROM common\_users  
SQL (0.000113) BEGIN  
4444444444444444444444444444  
User Load (0.000000) Mysql::Error: Unknown column  
‘common\_users.email’ in ‘where clause’: SELECT \* FROM common\_users  
WHERE (common\_users.email = ‘removed\_email\_address@domain.invalid’) LIMIT 1  
SQL (0.000109) ROLLBACK  
888888888888888888888

The table name looks fine, but the field is still being referred to as  
‘email’ here. I guess I’m a bit confused as to why these two  
statements are running at all?

Again, thanks for the help in advance.

On 4/20/06, Jean-François [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > “comment\_” from the front of every field name in the whole db! It
> 
> and do the same for the other fields. Maybe a sort of model scaffolding
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
[http://jimhalberg.com](http://jimhalberg.com)

---

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [April 21, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/integrating-with-legacy-databases/58363/4 "2006-04-21T17:41:08Z")

</div>

I found the culprit… I had missed this guy hanging out in a cluster  
of validation lines:  
validates\_uniqueness\_of :email, :on =\> :create

… causing the execution of that select with the field name “email”.

Thanks a ton for the help on this Jean-François.

- Jim

On 4/20/06, Jean-François [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I have the code (as you have detailed below) in my user.rb:  
> > def signup  
> > You can check that by unit-testing your model. Test a
> 
> …  
> user
> 
> –  
> À la renverse.

–  
[http://jimhalberg.com](http://jimhalberg.com)

---

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [April 22, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/integrating-with-legacy-databases/58363/5 "2006-04-22T18:49:24Z")

</div>

Jim H. wrote:

> ActiveRecord::Base.primary\_key\_prefix\_type = :table\_name\_with\_underscore  
> “comment\_author\_email” all over my whole application. and since this  
> will be in a more complete environment: I also don’t want to remove  
> “comment\_” from the front of every field name in the whole db! It  
> will need to be specific to the Comment class itself.
> 
> Any help is most appreciated. Thanks.
> 
> - jim

I did exactly just that for my presentation at Canada on Rails…

[http://rubyurl.com/DRv](http://rubyurl.com/DRv)

-Robby

---

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [April 21, 2006, 12:14am UTC](https://www.ruby-forum.com/t/integrating-with-legacy-databases/58363/6 "2006-04-21T00:14:22Z")

</div>

Jim :

> def email  
> return if generate\_blank  
> @params[‘user’].delete(‘form’)  
> @user = User.new(@params[‘user’])  
> […]

In fact, I think there may be a problem with User#new  
when passing a hash, since the constructor creates the User  
instance with the help of the column definitions. As we’ve  
got a facade column (let’s say faked attribute), there must be  
trouble. The User#create must work correctly I think.  
You can check that by unit-testing your model. Test a  
User.new with a hash having a ‘email’ key.

If I’m right, it should fail. Instead,

user = User.new  
user.new\_password = ‘…’  
user.email = ‘…’  
user.save

must work (another test)

So test also User#create.

For your code, maybe a way will be :

h = params[:user]  
h.remove(‘form’)  
email = h[:email]  
h.remove(:email)  
@user = User.new(h)  
@user.email = email  
…

And for convenience, make it a method

class User \< AR::B

# maybe not a good idea to override AR::B#initialize

def self.my\_special\_new(hash)  
email = hash[:email]  
hash.remove[:email]  
user = new(hash)  
user.email = email  
user  
end  
…  
end

sth like that… (to be unit-tested)

Hope this helps,

– Jean-FranÃ§ois.
