如何在数据库中获取原始的“created_at"值(不是转换为 ActiveSupport::TimeWithZone 的对象) [英] How to get the raw 'created_at' value in the database (not an object cast to an ActiveSupport::TimeWithZone)
问题描述
假设我有一个帖子模型.
Imagine I have a Post Model.
数据库中的一条记录将是:
and one record in the database will be:
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 字段.
and the last two field (2011-02-20 08:01:55.583222|2011-02-20 08:01:55.583222
) are the created_at and updated_at field.
那么,
post = Post.find_by_id(23)
问题:如何在数据库中获取 created_at 字符串:2011-02-20 08:01:55.583222
"?
Question: How can I get the created_at string: "2011-02-20 08:01:55.583222
" in the data base?
我们知道,post.created_at 将返回一个 ActiveSupport::TimeWithZone
对象,而不是原始字符串.
As we know, the post.created_at will return a ActiveSupport::TimeWithZone
object, not the raw string.
请帮忙:)
推荐答案
使用 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
根据在类型转换之前访问属性.
这篇关于如何在数据库中获取原始的“created_at"值(不是转换为 ActiveSupport::TimeWithZone 的对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!