0
votes

So I have an existing database that I'm trying to manipulate through ActiveRecord. All of the column, table, and database names are camel-cased, ie. myColumnOne, etc. This doesn't really pose a problem except that, when I was trying to define the associations between the tables, I can't get Rails to accept and leave my input for the foreign key names alone.

For example: I'm trying to define an association between a table called cableModems and a table called cmModels, where cableModem has_one :cmModel, :foreign_key => "cmModelId"

Rails seems to want to say that the foreign_key is actually cm_model_id. Even if I input it the way I showed above.

My question is this: Is there any way to make Rails accept the casing? Or do I need to go about this without the associations?

Many Thanks!

2

2 Answers

0
votes

After digging through the Ruby on Rails API docs to trace the execution path for has_one, it's quickly become fairly clear that there is no quick and easy way to prevent the automatic de-camelCasing. With named scopes and one or two custom methods, you should be able to replicate the functionality of has_one fairly easily, so I would suggest foregoing the association for now, and possibly submit that as a ticket on the Rails Lighthouse.

0
votes

Is your database really case-dependent? Wouldn't it just work if you wrote

has_one :cm_model, :foreign_key => "cmmodelid"

I would also make write your classes as follows:

class CableModem < ActiveRecord::Base
  set_table_name "cableModems" 
  set_primary_key "cableModemId" 
end

Does that help in any way?