2013-04-20 40 views
0

你好我有一个应用程序在rails中,我需要从对象中检索创建日期。 我知道rails使用时间戳来自动保存这些信息,我看着.json ant它拥有Created_at信息。现在的问题是我如何从对象访问该信息(Created_at)(我的本意是创造时间,并显示这个太顺序)如何从轨道中的对象获取创建日期?

韩国社交协会任何人的帮助

+0

你能告诉我们你现在有?帮助你更容易。 – fmendez 2013-04-20 16:41:23

回答

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