ruby-on-railspluck

How to format date when using pluck in Rails


My User model looks like this. User has many attached Resume.

class User < ApplicationRecord
  has_many_attached :resumes, dependent: :destroy
end

Now in different form, I want to show list of resumes upload by user in descending order. I manage to do that by using following code.

<%= 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' %>

So now in my select drop down I am able to see all the uploaded resume in descending order. My dropdown looks like this file_name.pdf - 2021-11-30 03-59-59 UTC. I don't want to show time in dropdown. I just want to format it and show date with filename, something like this john_resume.pdf 11/11/2021.

So my question is how can I format the date when using pluck ?


Solution

  • <% 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' %>