在 rails 模型中手动设置列名 [英] Manually set column name in rails model

查看:36
本文介绍了在 rails 模型中手动设置列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在围绕几个现有数据库构建一个 rails 应用程序,现有数据库中使用的列名与 rails 关联约定根本无法正常工作.有没有办法在模型中设置列名别名,就像你可以

I'm building a rails app around several existing databases, the column names used in the existing databases do not work well at all with the rails association conventions. Is there a way to set column name aliases in a model similar to the way you can

class User < Activerecord::Base
self.set_table_name "users"
end

当现有的 db 列不能使用默认的 rails 关联命名约定时,我可以设置列名别名吗?

Could I set column name aliases when the existing db columns will not work with default rails association naming conventions?

推荐答案

在您的模型中,只需为属性(列)设置别名.例如:

In your model, just setup alias for attributes (columns). For example:

class User < Activerecord::Base
  alias_attribute :new_column_name, :real_column_name
end

这篇关于在 rails 模型中手动设置列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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