3

I have the following code, within which I want to change certain values to csv friendly, e.g., 'nil' to ''. I need to know how to make these changes. Thank you.

  def daily_door_schedule
    @tickets = Ticket.where(active: true).
      pluck(
        :door_manufacturer,
        :job_number, 
        :door_style, 
        :door_allocation_date,
        :date_doors_received_in_aub, 
        :door_delivery_due_date,
        :notes
      )

    respond_to do |format|
      format.html
      format.csv { render text: @tickets.to_csv }
    end
  end
3
  • 1
    to_csv can handle nil values, it results in an empty field Commented Sep 8, 2015 at 19:36
  • What is the specific problem? The CSV class doesn't create strings containing the word 'nil'. Commented Sep 8, 2015 at 19:42
  • 2
    Ruby doesn't have methods where or pluck. If they're not defined in Ticket, you need at least one more tag. Commented Sep 8, 2015 at 19:59

1 Answer 1

3

This should do it:

@tickets = Ticket.where(active: true).
  pluck(
    :door_manufacturer,
    :job_number, 
    :door_style, 
    :door_allocation_date,
    :date_doors_received_in_aub, 
    :door_delivery_due_date,
    :notes
  ).map { |ticket| ticket.map(&:to_s) }
Sign up to request clarification or add additional context in comments.

2 Comments

Anthony, this is what happens when you don't use your reading glasses.I think you want el ||= ''. If el is a string or nil, you could also write ticket.map(&:to_s), but I guess you want strings anyway. (I say this without knowing what pluck does.)
I just went to the optometrist for the first time in my life 2 weeks ago... your comment couldn't be more appropriate. As always, thank you for the suggestion - it's perfect.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.