# Column Names in Legecy Tables

**URL:** <https://www.ruby-forum.com/t/column-names-in-legecy-tables/74393>\
**Category:** Rails\
**Created:** [November 6, 2006, 11:29am UTC](https://www.ruby-forum.com/t/column-names-in-legecy-tables/74393 "2006-11-06T11:29:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dinesh](https://www.ruby-forum.com/u/dinesh)\
**Post date:** [November 6, 2006, 11:29am UTC](https://www.ruby-forum.com/t/column-names-in-legecy-tables/74393/1 "2006-11-06T11:29:14Z")

</div>

Hi,

I’m using Rails with DB2 Database. The tables in the database are NOT  
rails conventions, and there is no ‘id’ column too in the tables.

Hnece in the Model i’m setting the table name like this

ModelEmployee.set\_table\_name “employee”

Now if i try to print the colulmn names, it does not print anything, i  
have tried to print the column name as below, but its not printing  
anything.

ModelEmployee.set\_table\_name “employee”  
puts ModelEmployee.column\_names  
puts ModelEmployee.content\_columns

Hence if we are using Legecy Database, how should we access the column  
names of the corresponding model?

Thanks.  
Dinesh

---

<div class="post-metadata">

**Author:** ![dinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dinesh](https://www.ruby-forum.com/u/dinesh)\
**Post date:** [November 7, 2006, 11:06am UTC](https://www.ruby-forum.com/t/column-names-in-legecy-tables/74393/2 "2006-11-07T11:06:09Z")

</div>

maybe like this :

class Employee \< ActiveRecord::Base  
set\_table\_name “employee”

end

the in irb:

puts Employee.column\_names

maybe show us the table structure, would be helpfull to see it,  
concering primary key setup and the like …

---

<div class="post-metadata">

**Author:** ![dinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dinesh](https://www.ruby-forum.com/u/dinesh)\
**Post date:** [November 7, 2006, 11:33am UTC](https://www.ruby-forum.com/t/column-names-in-legecy-tables/74393/3 "2006-11-07T11:33:16Z")

</div>

Dinesh wrote:

> I’m using Rails with DB2 Database. The tables in the database are NOT  
> rails conventions, and there is no ‘id’ column too in the tables.

Can you use the SQL VIEW statement to rename things to Rails’s liking?  
VIEW is very powerful, and could fix the “problem” at its source.

In a multi-layered system like a website stack, I always seek to solve  
the problem in the most convenient (and efficient) layer…

–  
Phlip

---

<div class="post-metadata">

**Author:** ![dinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dinesh](https://www.ruby-forum.com/u/dinesh)\
**Post date:** [November 8, 2006, 1:17am UTC](https://www.ruby-forum.com/t/column-names-in-legecy-tables/74393/4 "2006-11-08T01:17:36Z")

</div>

Phlip wrote:

> Dinesh wrote:
> 
> > I’m using Rails with DB2 Database. The tables in the database are NOT  
> > rails conventions, and there is no ‘id’ column too in the tables.
> 
> Can you use the SQL VIEW statement to rename things to Rails’s liking?  
> VIEW is very powerful, and could fix the “problem” at its source.
> 
> In a multi-layered system like a website stack, I always seek to solve  
> the problem in the most convenient (and efficient) layer…
> 
> –  
> Phlip

You could use the DrySQL Ruby plugin: http://drysql.rubyforge.org

It will automatically identify all your keys, and generate all the  
associations and validations for your ActiveRecord data class, even if  
you do not follow the ActiveRecord naming conventions.

All you need to do is name your table according to ActiveRecord naming  
conventions OR call set\_table\_name in your class definition, and DrySQL  
handles the rest.
