notaceo
notaceo

Reputation: 1093

How do I pull each element of the array out using the Comma gem?

I'm using the Comma gem with Rails. Here's my Customer Model:

class Customer
  include MongoMapper::Document

  key :customer_id, Integer
  key :company_id, String
  key :first_name, String, :required => true
  key :last_name, String
  key :email, Array, :unique => true, :required => true
  key :phone, Array

  comma do 
    first_name
    last_name
    email
    phone
  end
end

Then I render:

format.csv { render :csv => @all_customers }

@all_customers is just a collection of the customers for this particular environment—this part works fine.

When I export to CSV, I get these types of entires:

["[email protected]"]

I know that's because email is an array, and Comma is just literally rendering the entire thing. I want:

[email protected]

Normally I would do something like:

email[0]

And it would grab just that value from the array. But trying this resulted in no difference in the exported CSV. Still had the unnecessary brackets and quotes.

How do I just grab the element and avoid the brackets and quotes?

Upvotes: 1

Views: 92

Answers (1)

xdazz
xdazz

Reputation: 160963

If you only want the first email, then

comma do 
    first_name
    last_name
    email do |email| email[0] end
    phone
end

Or if you want all the emails, then

comma do 
    first_name
    last_name
    email do |email| email.join(',') end
    phone
end

Upvotes: 1

Related Questions