ruby on rails - Problem: Cannot create mulitple table entries in an each-loop -


i'm trying loop through array in controller create new users array of email-adresses

@emails.each |email|   @user = user.new(:email => email)   @user.save end 

now creates 1 user , not several array contains.

i suspect, somehow have reinitialize new user differently controller handle on instance of it. doing wrong?

are sure it's creating 1 user in db? code looks ok, @user ever refer single instance because re-set on each iteration through array.

if you're wanting array of users @ end, better way of doing add them array (you use inject this):

@users = [] @emails.each |email|   @users << user.create(:email => email) end 

another reason due validations making record invalid. have validations on email? if want make blow if record invalid, use save or create bang (!)...

@emails.each |email|   user.create!(:email => email) end 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -