Rails中的Postgresql数据库中的camelCase列(ActiveRecord) [英] camelCase column in postgresql database in rails (ActiveRecord)

查看:59
本文介绍了Rails中的Postgresql数据库中的camelCase列(ActiveRecord)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Post模型.它具有标题和正文.我想在其中添加简短说明.虽然我知道如何使用snake_case(即short_description)进行操作,但我想知道是否有任何方法可以使用以下方法创建帖子camelCase(即shortDescription).

I have Post model. It has title and body. I want to add short description in there. Although, I know how to do it with snake_case (i.e. short_description), I wonder if there is any way to create a post with camelCase (i.e. shortDescription).

这是我的schema.rb文件

This is my schema.rb file

  create_table "posts", force: :cascade do |t|
    t.datetime "created_at",                     null: false
    t.datetime "updated_at",                     null: false
    t.string   "title"
    t.text     "body"
    t.string   "shortDescription"
  end

我在控制器和表单中添加了shortDescription.当我转到posts#create时,出现以下错误.

I added shortDescription to controller and a form. When I go to posts#create, I get the following error.

未定义方法`shortDescription'

现在,我将回滚数据库并创建short_description.这不是一个大问题,但是由于我对这项技术的好奇心,我想知道如何使用camelCase.(也许有一天我需要在Rails应用程序中加入带有camelCased属性的数据库).

For now I am going to rollback the database, and create short_description. It is not a big issue, but because of my curiosity for the technology, I would like to know how to use camelCase. (Maybe someday I need to join the db with camelCased attributes with Rails app).

任何建议,我们将不胜感激.谢谢!

Any advise is appreciated. Thank you!

推荐答案

在Postgres(以及SQL语言的ISO/ANSI标准)中,对象名称不区分大小写.

In Postgres (as well as in the ISO/ANSI standard of SQL language), object names are case-insensitive.

因此 objectName objectname 相同,并且在决定使用驼峰式命名时必须考虑到这一点.

So objectName is the same as objectname, and you must take it into account when deciding to use camel-cased names.

您可以告诉Postgres,您确实要使用区分大小写的名称-只需在名称周围加上双引号即可:"objectName" .请记住,以后您将无法使用 objectName 之类的对象,它只会尝试查找 objectname 而不会找到它,从而引发错误,因此必须使用双引号.

You can tell Postgres, that you do want to use case-sensitive name – just add double quotes around the name: "objectName". Keep in mind, that later you won't be able to use such object as objectName, it will simply try to find objectname and won't find it, triggerring an error, so using double quotes will be mandatory.

此外,在使用双引号区分大小写的对象名称时,还有一些小的警告(例如,psql的 \ d 命令将这样列出您的对象:"public.objectName",这不是真的正确,正确的名称是" public." objectName"等).

Also, there are some minor caveats when working with double-quoted case-sensitive object names (for instance, psql's \d command will list your object like this: "public.objectName", which is not really correct, the correct name is "public"."objectName", etc).

在少数项目中,我使用了骆驼样式的表/列名称,这总是有些痛苦,尤其是当新开发人员开始使用此类项目时.

In few projects, I had camel-style table/column names and it always was some pain, especially when a new developer started to work with such project.

因此,我建议始终在SQL中使用下划线名称( object_name ).

So I'd suggest using underscorded names in SQL always (object_name).

这篇关于Rails中的Postgresql数据库中的camelCase列(ActiveRecord)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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