如何获取数据库中的原始’created_at’值(不是转换为ActiveSupport :: TimeWithZone的对象)
想象一下,我有一个Post Model。
并且数据库中的一条记录将是:
23|title_here|content_here|2011-02-20 08:01:55.583222|2011-02-20 08:01:55.583222
最后两个字段( 2011-02-20 08:01:55.583222|2011-02-20 08:01:55.583222
)是created_at和updated_at字段。
然后,
post = Post.find_by_id(23)
问题 :如何在数据库中获取created_at字符串:“ 2011-02-20 08:01:55.583222
”?
我们知道,post.created_at将返回一个ActiveSupport::TimeWithZone
对象,而不是原始字符串。
请帮忙 :)
使用attributes_before_type_cast
Post.find(23).attributes_before_type_cast["created_at"]
要么
Post.find(23).read_attribute_before_type_cast("created_at")
编辑
你也可以这样打电话:
Post.find(23).created_at_before_type_cast
根据访问属性之前他们已经被typecasted链接。
尝试
Post.find_by_id(23).created_at.to_s