[Rails] Difference between create method and new + save method

environment

・ Rails 6.0.3.2 ・ Mysql Ver 14.14 Distrib 5.6.47 ・ Osx10.15

Synopsis

Learning Contoller with Rails will learn ʻActive Record methods such as new, save, create, destroy. At that time, I learned that the create method is new+save`.

As a beginner, I misunderstood that "then create is simpler and easier to read, so I use Kochi! ", And when I created my own app, sometimes it didn't work well with create. I will write what I learned at that time.

Why divide into new + save?

In short, when to use new + save without using create method in create action? For example, the description in the controller

def create
  @message = Message.create(message_params)
end

not

def create
  @message = Messages.new(message_params)
  @message.save
end

here.

① When writing a conditional statement using if etc.

def create
    @message = Messages.new(message_params)
    if @message.save
      redirect_to(Applicable path)
    else
      flash.now[:alert] = 'Please enter your message.'
      render :index
    end
end

If you use create when using the conditional statement as described above, the conditional statement does not work well and the operation does not work well when the creation fails after ʻelse`.

Also, there is a point that it is easier to see if there are many descriptions.

② When using render

If you describe new, you can use the instance method created at that time with render. Although it is not in the above example, it is better to describe new even if you want to inherit the: id and use it.

Finally

I was wondering how they would be different if they were said to have the same meaning, and I made some mistakes, so I wrote them here.

Recommended Posts

[Rails] Difference between create method and new + save method
[Rails] I studied the difference between new method, save method, build method and create method.
[Rails / ActiveRecord] About the difference between create and create!
Rails: Difference between resources and resources
Difference between new and create in Rais action controller
Difference between instance method and class method
Difference between render method and redirect_to
Difference between == operator and eqals method
[Rails] Difference between find and find_by
[rails] Difference between redirect_to and render
[Rails] Difference between redirect_to and render [Beginner]
Difference between member and collection of rails routes.rb
Difference between vh and%
Difference between i ++ and ++ i
[Rails] What is the difference between redirect and render?
[Rails] I investigated the difference between redirect_to and render.
Difference between product and variant
Difference between redirect_to and render
Difference between puts and print
[Rails] require method and permit method
Difference between redirect_to and render
Rails "render method" and "redirect method"
Difference between CUI and GUI
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
What is the difference between an action and an instance method?
Let's override the difference between == (identity) and equals method (equivalence)
[Ruby] Difference between get and post
Difference between interface and abstract class
[Rails] Save start time and end time
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Rough difference between RSpec and minitest
Create a new app in Rails
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[Android] Difference between finish (); and return;
Install Rails in the development environment and create a new application
Easy to understand the difference between Ruby instance method and class method.
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
[Rails6] Create a new app with Rails [Beginner]