(Rails) Is there a way to check the field's datatype?

前端 未结 2 1210

How do you check what the datatype is for something that was retrieved from the database?

For example, if I have some instantiation of a model @model with a database

2条回答
  •  伪装坚强ぢ
    2021-01-07 18:34

    Try this:

    @model.column_for_attribute('title').type
    

    Should return :string, :text, :integer, etc.

    The ActiveRecord Column class also includes a number of other attributes: default, limit, name, null, precision, primary, scale, sql_type, type.

提交回复
热议问题