Nosayr Yassin
Nosayr Yassin

Reputation: 419

parsing in rails using CSV

So I am trying to parse a list of emails separated by a comma and inputted in a form using the built-in CSV library in rails 3.

Here is the code:

@variable = params[:body]
@csv = CSV.parse(@variable, :col_sep => ",")
  @csv.each do |row|
    user = User.where(:email => row)

However, the output is something like this:


- - [email protected]
  - ! ' [email protected]'

Therefore, each row in the @csv that I am trying to go over is an entire list of emails. How can I separate them? Thanks!

Upvotes: 1

Views: 1979

Answers (1)

Alex Blakemore
Alex Blakemore

Reputation: 11919

CSV::parse returns an array of arrays if you don't pass a block as an argument. So when you enumerate @csv using #each, the argument row will be an array of strings. Not a single string as your code suggests.

If the email address is the first column of each row in your CSV file, then you would replace

User.where(:email => row)

with

User.where(:email => row[0])

It's not exactly clear what the format of your CSV file is though. If you have multiple email addresses per row, or have the email address in a different column, you'll have to revise the code accordingly.

Upvotes: 1

Related Questions