The difference between โ€œsaveโ€ and โ€œupdateโ€ when used with various HTTP requests

When I tried replacing @post.update with @post.save , as in my code below, it still worked, and it returned true, but the values โ€‹โ€‹were not updated.

  def create @post = Post.new(post_params) if @post.save redirect_to posts_path, notice: 'Post was successfully created.' else render action: 'new' end end def update respond_to do |format| if @post.update(post_params) format.html { redirect_to @post, notice: 'Post was successfully updated.' } format.json { head :no_content } else format.html { render action: 'new' } format.json { render json: @post.errors, status: :unprocessable_entity } end end end 

Below are my rake routes:

 $ rake routes posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy root / welcome#index 

Why didn't he update or rewrite my record?

Will using different HTTP requests for the same methods affect them? Can we use PUT , GET , PATCH and DELETE to save when passing with the appropriate syntax?

The question is about guides 4 guides, the first guide .

+6
source share
3 answers

Because save will not accept attributes as parameters; save can only accept parameters like validate: false to skip validation.

If you want to use save , you need to assign or change individual attributes before save . But if you want mass assignment, update will be your choice.

 @post.f_name = 'foo' @post.l_name = 'bar' @post.update # This will not work @post.save # This will work @post.save({:f_name=>"peter",:l_name=>"parker"}) # This will not work @post.update({:f_name=>"peter",:l_name=>"parker"}) # This will work 
+11
source

[1] http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html provides a good explanation.

save (*) link

Saves the model.

If the model is new, the record is created in the database; otherwise, the existing record is updated.

By default, saving always triggers checks. If any of them does not work, the action is canceled, and the save returns false. However, if you set validate: false, the checks are completely excluded.

save! (*) Link

Saves the model.

If the model is new, the record is created in the database; otherwise, the existing record is updated.

Saved! checks are always performed. If any of these fail ActiveRecord :: RecordInvalid gets promoted.

update (attributes) Link

Updates the model attributes from the passed hash and saves the record, all wrapped in a transaction. If the object is invalid, the save will fail and false will be returned. Also an alias: update_attributes

update! (attributes) Link

Updates the receiver in the same way as updating, but causes a save! instead of saving, so an exception occurs if the entry is invalid. Also an alias: update_attributes!

+4
source

To be precise: 1. Save will save all instances of the class that may not be required for this. Especially when validation is present. for example, @ post.save It will update all the attributes or column for the post.

  • The update will update only one attribute of the class or the corresponding model of this class @ Post.update. (: Condition, present)
0
source

All Articles