我怎样才能获得一个属性从一个Rails模型的数据类型? [英] How can I get the datatype of an attribute from a Rails model?

查看:134
本文介绍了我怎样才能获得一个属性从一个Rails模型的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails项目的Postgres,我还发现,我需要改变我所有的 VARCHAR 的数据类型为 citext 。而不是通过手工做到这一点,我只想创建循环遍历所有的模型和它们的属性,并在必要时将它们转换迁移。

大多数机型都是空的,所以它不是实例化他们的问题。我需要找出是否ActiveRecord的了解及其相应的数据库字段的数据类型为。

解决方案

  @ model.column_for_attribute(标题)。类型
 

这可能是你要寻找的。

I am using Postgres in a Rails project and I have discovered that I need to change all of my varchar datatypes to citext. Rather than do this by hand, I want to just create a migration that loops through all of the models and their attributes and converts them as necessary.

Most of these models are empty, so it's not a matter of instantiating them. I need to find out if ActiveRecord "knows" what the datatype of its corresponding database column is.

解决方案

@model.column_for_attribute('title').type

That might be what you're looking for.

这篇关于我怎样才能获得一个属性从一个Rails模型的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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