How can I get the datatype of an attribute from a Rails model?

空扰寡人 提交于 2019-12-25 07:29:44

问题


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.


回答1:


@model.column_for_attribute('title').type

That might be what you're looking for.



来源:https://stackoverflow.com/questions/16321032/how-can-i-get-the-datatype-of-an-attribute-from-a-rails-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!