![](/img/trans.png)
[英]Rails 5 query using Pluck how do I format created_at to be date only?
[英]How to format date when using pluck in Rails
我的用戶 model 看起來像這樣。 用戶有許多附加的簡歷。
class User < ApplicationRecord
has_many_attached :resumes, dependent: :destroy
end
現在以不同的形式,我想按降序顯示用戶上傳的簡歷列表。 我設法通過使用以下代碼來做到這一點。
<%= f.select :resume, options_for_select(@user.resumes.includes(:blob).references(:blob).pluck(:filename, :created_at).reverse.map{ |e| e.join(' - ') }), prompt: '-- Select --', class: 'form-control' %>
所以現在在我的 select 下拉菜單中,我可以看到所有上傳的簡歷按降序排列。 我的下拉列表看起來像這個file_name.pdf - 2021-11-30 03-59-59 UTC
。 我不想在下拉列表中顯示時間。 我只想格式化它並用文件名顯示日期,比如john_resume.pdf 11/11/2021
。
所以我的問題是如何在使用 pluck 時格式化日期?
<% options = @user.resumes.includes(:blob).references(:blob).pluck(:filename, :created_at).reverse.map { |e| filename = e.first; created_at = e.last; [filename, created_at.strftime('%d/%m/%Y')].join(' - ') }%>
<%= f.select :resume, options_for_select(options), prompt: '-- Select --', class: 'form-control' %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.