Rails:从tinyint读取整数

TTT*_*TTT 2 ruby mysql database ruby-on-rails ruby-on-rails-3

我有一个RoR + MySQL设置.在数据库中有一个Tinyint字段t.当我从Rails 读取t的值时,我得到truefalse,因为Tailsint字段被Rails解释为boolean.到现在为止还挺好.

如何从Rails中读取此字段t作为实际整数?

小智 5

http://apidock.com/rails/ActiveRecord/AttributeMethods/BeforeTypeCast/attributes_before_type_cast

例如:

Model.first.attributes_before_type_cast['your_boolean_field']
Run Code Online (Sandbox Code Playgroud)

这取决于你的DB将返回什么值,在postgesql中它是'f'或't'