0

I'm trying to take everything out of my Users table and send it to mail chimp for subscription.

I need to go from User.all to

[{:email => {:email => "[email protected]"}, 
  :merge_vars => {:FNAME => "First name", :LNAME => "Last name"}
}]

I'm trying to do this with map but I'm struggling, any ideas of the cleanest way of doing this?

2 Answers 2

1
subscription_array = User.all.collect do |user| 
 {
  :email => {:email => user.email}, 
  :merge_vars => {:FNAME => user.first_name, :LNAME => user.last_name}
 }
end
Sign up to request clarification or add additional context in comments.

Comments

1
users_array = []
User.all.find_each {|u| users_array << {:email => u.email, :merge_vars => {:FNAME => u.first_name, :LNAME => u.last_name} }

I chose to use find_each is it's a more efficient way of loading your collection.

http://api.rubyonrails.org/classes/ActiveRecord/Batches.html

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.