T
T
therhino2015-02-25 00:55:48
Ruby on Rails
therhino, 2015-02-25 00:55:48

Why doesn't the author check the existence of an object in the database before deleting it?

I've seen code like this many times:

def destroy
    user = User.find params[:id]
    user.destroy!
    render nothing: true, status: 204
  end

  1. Why does the author not check the presence of this user in the database?
  2. What happens if it doesn't exist? Will the server throw an error on its own?
  3. How to set up an application to work with server errors? (500, 501, ...)
  4. How would it be correct to write this code? What to return to the user? (this is the API code)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
C
caution, 2015-02-25
@caution

because the code will fall user = User.find params[:id] - on this line it will not reach the end yet. this is a kind of test.

S
Semyon Semyonov, 2015-02-25
@man_without_face

So these are just examples...

S
Shaks, 2015-02-25
@shaks

1. Because, as Semyon Semyonov said , these are just examples.
2. An exception will occur ActiveRecord::RecordNotFound
3. The question is not clear, what does it mean to configure server errors?

V
Viktor Vsk, 2015-02-25
@viktorvsk

api.rubyonrails.org/classes/ActiveSupport/Rescuebl...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question