2013-04-20 79 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