What does this do in Rails?
create! do |user| #initialise user end
I decided that it creates user objects and stores it in the database. How does this differ from the simple expression user.new(...) and user.save() ?
user.new(...)
user.save()
In a nutshell:
create!
create
save!
save
true
false
new does not save. new similar to the ActiveRecord context build . create saves the database and returns true or false depending on the model validation. create! saves the database, but throws an exception if there are errors in the verification of the model (or any other error).
new
ActiveRecord
build
When failed to create a record, create! throws an exception, new and then save (or just create without an exclamation point) exit silently.
create accepts attributes, so using a block here is somewhat unusual. The code you mentioned performs initialization in the block that is passed to create! It basically coincides with new , followed by initialization, and then a save!
There are many options for saving, saving !, creating, terminating !, updating, updating !, etc. there are also variations in terms of validations and backlinks
See API for more details: (this is discussed in the first link)
http://api.rubyonrails.org/classes/ActiveRecord/Base.html
http://apidock.com/rails/ActiveRecord/Base
http://m.onkey.org/active-record-query-interface
Source: https://habr.com/ru/post/901183/More articles:rake jasper report - jasper-reportsCreating API documentation from RESTEasy annotations - javadocFailed To See Axis2 Eclipse Plugin - eclipseIs it possible to transfer the json format in the address bar of the browser url? (GET method) - jsonHow to create a 7 "tablet (1280 * 800) screen resolution screen in Android? - androidfirefox does not vertically center selected input content with specified height - cssExtensible implementations of the Provisioning Protocol? - cCustom 500 error page using JSF - is this a complete error message? - javaDelphi 7 - Handling MouseWheel events for inline frames in forms? - delphihttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/901188/whats-the-reason-behind-using-ul-li-lists-to-draw-navigation-bars&usg=ALkJrhiN6_o66s5WQmw5zNnIZ_FfhNX0MgAll Articles