Ruby on Rails:是否可以使用 Camel-cased 数据库字段和表名? [英] Ruby on Rails: Is it possible to use Camel-cased database field and table names?

查看:39
本文介绍了Ruby on Rails:是否可以使用 Camel-cased 数据库字段和表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby on Rails 数据库表和字段名称的约定是 snake_case 而不是 CamelCase.但是,我有一个 PHP 应用程序正在使用的现有数据库.我想编写一个与该数据库交互的 Rails 应用程序.是否有一种简单的Rails 方式"可以使用 CamelCase 数据库表和字段名称与数据库交互?

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 would like to write a Rails application that interacts with this database. Is there an easy, "Rails way" to interact with a database using CamelCase database table and field names?

推荐答案

简短的回答是肯定的,但并不总是比将旧数据库迁移到新数据库容易.如果您希望两个应用程序能够使用相同的数据库,那么这可能是最快捷的方法.

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"

所有的 AR 关系也可以设置主键字段.

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.

这篇关于Ruby on Rails:是否可以使用 Camel-cased 数据库字段和表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆