# Case Sensitivity and ID Field Naming

**URL:** <https://www.ruby-forum.com/t/case-sensitivity-and-id-field-naming/57128>\
**Category:** Rails\
**Created:** [April 7, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/case-sensitivity-and-id-field-naming/57128 "2006-04-07T12:02:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 7, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/case-sensitivity-and-id-field-naming/57128/1 "2006-04-07T12:02:28Z")

</div>

Hi,

I am working with an existing database that has table and field names  
such as ‘TblUsers’ and ‘txtEmailAddress’. Is rails sensitive to the case  
of the letters here? or can I simply refer to these tables and fields as  
‘tblusers’ and ‘txtemailaddress’ in my controllers ad rhtml files?

Also, the database has id fields named after the tables such as  
‘TblUsersID’ for ‘TblUsers’. I know rails prefers id fields to be named  
‘id’. How difficult is it to override this?  
If I can override it, does every instance of ‘id’ become ‘TblUsersID’?  
eg. in the following example, would this be correct?

**Title**

Thank you in advance!

Alana

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 7, 2006, 12:52pm UTC](https://www.ruby-forum.com/t/case-sensitivity-and-id-field-naming/57128/2 "2006-04-07T12:52:37Z")

</div>

On Friday 07 April 2006 12:02, Alana wrote:

> If I can override it, does every instance of ‘id’ become ‘TblUsersID’?
> 
> Thank you in advance!
> 
> Alana

In every model, you can execute set\_primary\_key. So, for your  
‘TblUsers’, you  
can do

set\_primary\_key(:TblUsersID)

When you have done that, you can call .id on your objects, because it  
maps to  
the primary key. Calling “tbluserid” as you did in your example is not  
necessary (it won’t even work I guess, mostly because it should be  
TblUsersID…), the primary key is always “id”.

And, it is indeed case sensitive. You could have tried that out easily  
🙂

BTW, you don’t have to specify the HTML tags for the form and fields  
yourself.  
If you make use of the helpers (like “text\_field”), it creates the  
necessary  
HTML for you. Also, if you adhere to the standard naming convention,  
handling  
the data becomes much easier. The text\_field method for example takes as  
the  
first two arguments the object and the method. If for example you use  
“post”  
and object and “title” as method, it will create a textfield  
post[title]. This  
way, when you have a lot of textfields for the object post, you can  
simply do  
“post.attributes = @params[“post”]”, to have them assigned all at once.  
But, I  
guess the latter you already knew, looking at your code.

See [http://api.rubyonrails.com/](http://api.rubyonrails.com/) for more info on the details of the  
methods I  
mentioned.

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 8, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/case-sensitivity-and-id-field-naming/57128/3 "2006-04-08T15:07:42Z")

</div>

On Fri, Apr 07, 2006 at 12:02:28PM +0200, Alana wrote:

> I am working with an existing database that has table and field names  
> such as ‘TblUsers’ and ‘txtEmailAddress’. Is rails sensitive to the case  
> of the letters here? or can I simply refer to these tables and fields as  
> ‘tblusers’ and ‘txtemailaddress’ in my controllers ad rhtml files?

Unfortunately, Rails is sensitive to the case of table and field  
names. I recently developed an application that had to deal with  
legacy table and field names, and also had to work with Postgres  
databases created with all lowercase names, and with Mysql databases  
created with studlycased names. I ended up developing my application  
to work against Postgres, and then adopting this code to work with  
Mysql by redefining the table names and accessors when a Mysql  
database was in use. This involved augmenting each model definition,  
as for instance:

```
class Jobmedia < ActiveRecord::Base
  Jobmedia.table_name = 'jobmedia'
  Jobmedia.primary_key = 'jobmediaid'

  if STUDLY
Jobmedia.table_name = 'JobMedia'
def volumename; self.VolumeName; end
...
  end
end

```

… and then determining if studlification was required in  
environment.rb:

```
STUDLY = ActiveRecord::Base.configurations[RAILS_ENV]['adapter'] == 

```

‘mysql’

It would be very handy if the Active Record framework provided some  
type of “downcase fieldnames” option when generating the setter and  
getter methods, but this approach got the job done for me. Best of  
luck.

– John Kodis.
