Rails:如何在使用activerecord导出为CSV时截断记录

时间:2015-11-30 21:14:12

标签: ruby-on-rails activerecord

我正在我的公司网站上工作,这是用铁轨创建的,我是新手。 已包含CSV导出器,以及包含日期和时间的validated_at列。我想将此字段的导出截断为仅日期。这是代码:

In [15]: def entropy_loopy(distrib):
    ...:     n = distrib.shape[0] #n is the number of data points
    ...:     kld = np.zeros((n, n))
    ...:     for i in range(0, n):
    ...:         for j in range(0, n):
    ...:             if(i != j):
    ...:                 kld[i, j] = stats.entropy(distrib[i, :], distrib[j, :])
    ...:     return kld
    ...: 

In [16]: distrib = np.random.randint(0,9,(100,100)) # Setup input

In [17]: out = stats.entropy(distrib.T[:,:,None], distrib.T[:,None,:])

In [18]: np.allclose(entropy_loopy(distrib),out) # Verify
Out[18]: True

In [19]: %timeit entropy_loopy(distrib)
1 loops, best of 3: 800 ms per loop

In [20]: %timeit stats.entropy(distrib.T[:,:,None], distrib.T[:,None,:])
10 loops, best of 3: 104 ms per loop

端 我尝试过slice(9)并截断(0,9)但没有成功。我可以在日志中看到这些类型的错误:

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
]

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用to_date方法。

model.validated_at.to_date

或字符串格式strftime

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