Ruby on Rails: Is it possible to use Camel-cased database field and table names?

前端 未结 3 1688
无人及你
无人及你 2021-01-20 11:20

The Ruby on Rails convention for database table and field names is snake_case and not CamelCase. However, I have an existing database being used by a PHP application. I woul

3条回答
  •  旧时难觅i
    2021-01-20 11:27

    The short answer is yes but it's not always easier than migrating the old database to a new database. If you want both applications to be able to use the same database though then it is probably the quickest approach up front.

    You can override the table and foreign key fields by doing the following:

    set_table_name "camelCaseName" 
    set_primary_key "cameCaseIdName" 
    

    You can alias all the field names if necessary as well:

    alias "camelCaseFieldName", "field_name"
    

    All of the AR relationships can set the primary key field as well.

    has_many :comments, :foreign_key_id => "commentCamelCaseID"
    

    It's more work than normal but it is possible.

提交回复
热议问题