0

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 ?

r3b00t
  • 6,953
  • 6
  • 27
  • 37
  • You should avoid doing database queries in your view as much as possible - your views should just take data from the controller and render it in the simplest way possible. Views are messy mush of markup and ERB and this is recipe for very slow apps as you'll soon lose track of what database queries each action is performing. Move the query into the controller and you might want to reconsider using pluck as you'll have to parse the dates instead of just dealing with models. – max Nov 30 '21 at 06:21
  • Also instead of both `include` and `references` just use `eager_load`. Thats what happens anyways. – max Nov 30 '21 at 06:22

1 Answers1

1
<% 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' %>
Saiqul Haq
  • 2,287
  • 16
  • 18