・ Rails 22.214.171.124 ・ Mysql Ver 14.14 Distrib 5.6.47 ・ Osx10.15
Rails will learn ʻActive Record methods
such as new
. At that time, I learned that the create method
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.
In short, when to use
save without using
create method in
For example, the description in the controller
def create @message = Message.create(message_params) end
def create @message = Messages.new(message_params) @message.save end
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.
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.
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.