0

If I do:

Contact.select('email').first(1000).to_json 

I get a JSON response with emails... How can I get just a CSV of emails? [email protected], [email protected]

I'm trying to do this from rails console.

Thanks

2 Answers 2

2
Contact.select('email').map(&:email).join(", ")
Sign up to request clarification or add additional context in comments.

Comments

2

You can install this plugin to have the to_csv method. Or you can implement it yourself.

I would vote for the prior, because if you'll need to print out names instead of emails you should implement the escaping as well, and this would be there for you by the plugin.

Update:

Oh I have not realized that you need only the mails, with this plugin you could do the following:

@emails = Contact.select('email').first(1000).map(&:email)

respond_to do |format|
  format.html
  format.xml { render :xml => @emails }
  format.csv { send_data @emails.to_csv }
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.