Rails属性名称驼峰问题 [英] rails attribute names camelcase issue

查看:119
本文介绍了Rails属性名称驼峰问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有旧版Sql Server数据库和Rails应用程序。数据库中的列名称
都在PascalCase (FirstName,CustomerId ...)中。

I have legacy Sql Server database and Rails applications. Column names in database are all in PascalCase (FirstName, CustomerId...).

I正在寻找在Ruby中使用underscore_casing和在数据库中使用
PascalCasing的简便方法。我想在Rails
中为数据库中的FirstName列编写 first_name

I'm searching for an easy way to use underscore_casing in Ruby and PascalCasing in database. I would like to write first_name in Rails for FirstName column in database.

驼峰和下划线将将字符串转换为必需的大小写

camelize and underscore will convert a string to required casing

,但我正在寻找更通用的名称,例如:
ActiveRecord :: Base.camelize_table_column_names = true

but I'm looking for something more general like: ActiveRecord::Base.camelize_table_column_names = true

像现有的
ActiveRecord :: Base.pluralize_table_names

就像将rjs模板中的Ruby转换为JavaScript cameCase
约定一样。

Just like translating Ruby in rjs templates to JavaScript cameCase convention.

推荐答案

也许为所有定义的列名设置别名?

Maybe set up aliases for all defined column names?

column_names.each do |column_name|
  alias_attribute column_name.underscore, column_name
end

您可以将其添加到关注并在所有相关模型中包含该关注。

You could add that to a concern and include that concern in all relevant models.

这篇关于Rails属性名称驼峰问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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