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
Post a Comment