您好我在rails中有一个应用程序,我需要从对象中检索创建日期。 我知道rails使用时间戳来自动保存这些信息,我看着.json ant它有Created_at信息。问题是如何从对象访问此信息(Created_at)(我的意图是按创建时间排序,并显示此信息)
向任何帮助
答案 0 :(得分:1)
您可以通过以下方式访问该媒体资源:
u = User.first # Let's pretend there's a `User` model within the rails application. Here im getting the first record and storing that user in the variable `u`.
u[:created_at] # This will give me the value for the `created_at`, for instance: Thu, 18 Oct 2012 14:42:44 UTC +00:00
or
u.created_at # will give you the same result
如果您想要sort
该字段,您可以(假设有一个User
模型)使用sort_by
:
User.all.sort_by &:created_at # This is just a demonstration, you might want to get a sub-set of whatever model you're querying with `where` and some relevant criteria.
或
User.find(:all, :order => "created_at") # old and deprecated approach
或
User.order("created_at DESC") # more recent approach