What does this do in Rails?
create! do |user|
#initialise user
end
I figured it creates a user objects and saves it to the database. How is it different from just saying user.new(...) and user.save()?
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.
In a nutshell:
create!raises an exception whilecreatereturns the object (unsaved object if it does not pass validations).save!raises an error whilesavereturnstrue/false.savedoes not take attributes,createdoes.newdoes not save.newis similar tobuildinActiveRecordcontext.createsaves to the database and returnstrueorfalsedepending on model validations.create!saves to the database but raises an exception if there are errors in model validations (or any other error).