I am trying to save 2 rows, the 2nd row based on the 1st row:
user = User.new(....)
user.save!
user.id = nil
user.name = "different name"
user.save!
This doesn’t create a 2nd row, what should I do?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem is that Rails thinks you’re trying to change an instance that is already saved to the database, rather than create a new one. What you want to do is clone the original record, and it should work. Here are the Rails docs on using clone.