(Rails) 有没有办法检查字段的数据类型? [英] (Rails) Is there a way to check the field's datatype?
本文介绍了(Rails) 有没有办法检查字段的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查从数据库中检索到的数据类型是什么?
How do you check what the datatype is for something that was retrieved from the database?
例如,如果我有一个带有数据库字段title"的模型@model 的实例化,我希望能够编写类似@model.title.type 的代码并让它返回String".Rails 有任何内置功能吗?
For example, if I have some instantiation of a model @model with a database field "title", I want to be able to code something like @model.title.type and have it return "String". Does Rails have any built-in functionality for this?
推荐答案
试试这个:
@model.column_for_attribute('title').type
应该返回:string
、:text
、:integer
等
ActiveRecord Column 类还包括许多其他属性:默认、限制、名称、空值、精度、主要、比例、sql_type、类型.
The ActiveRecord Column class also includes a number of other attributes: default, limit, name, null, precision, primary, scale, sql_type, type.
这篇关于(Rails) 有没有办法检查字段的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文