2015-11-30 37 views
0

我正在使用rails創建的公司網站上工作,我是一名新手。 已經包含一個CSV導出器,以及包含日期和時間的validated_at列。我想截斷此字段的導出到僅限日期。下面是代碼:Rails:如何使用activerecord導出到CSV時截斷記錄

def build_user(user:) 
user = present(user) 

id = user.id 
first_name = user.first_name 
last_name = user.last_name 
email = user.email 
registration_date = user.created_at.strftime('%Y-%m-%d') 
marketing_communications_accepted = user.marketing_communications_accepted? 
completed_profile = user.ready_for_approbation? 
average_response_time = user.average_response_time 
blocked = user.blocked? 
confirmed_at = user.confirmed_at 
approved_at = user.approved_at 
blocked_at = user.blocked_at 

[ 
    id, first_name, last_name, email, registration_date, marketing_communications_accepted, 
    completed_profile, average_response_time, blocked, confirmed_at, approved_at, blocked_at 
] 

端 我試圖片(9)和截短(0,9)沒有成功。我可以在日誌中看到這些類型的錯誤:

> 2015-11-30T20:39:19.572933+00:00 app[worker.1]: 2015-11-30T20:39:19Z 3 TID-oul7oqy04 WARN: undefined method `slice' for Tue, 10 Nov 2015 15:01:33 EST -05:00:Time 
2015-11-30T20:39:19.573019+00:00 app[worker.1]: 2015-11-30T20:39:19Z 3 TID-oul7oqy04 WARN: /app/vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.11/lib/active_support/time_with_zone.rb:364:in `method_missing' 

感謝您的幫助。

回答

0

可以使用to_date方法。

model.validated_at.to_date 

或字符串格式strftime

model.validated_at.strftime('%Y-%m-%d') 
+1

完美工作 –

相關問題