How to get the raw 'created_at' value in the database (not an object cast to an ActiveSupport::TimeWithZone)

后端 未结 2 926
生来不讨喜
生来不讨喜 2021-01-07 16:48

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.583         


        
相关标签:
2条回答
  • 2021-01-07 17:28

    Try

    Post.find_by_id(23).created_at.to_s
    
    0 讨论(0)
  • 2021-01-07 17:41

    use attributes_before_type_cast

    Post.find(23).attributes_before_type_cast["created_at"]
    

    or

    Post.find(23).read_attribute_before_type_cast("created_at")
    

    Edit

    You can call like this also:

    Post.find(23).created_at_before_type_cast
    

    according to Accessing attributes before they have been typecasted.

    0 讨论(0)
提交回复
热议问题