How can I get the datatype of an attribute from a

2019-09-05 03:56发布

问题:

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.