ActiveRecord 'destroy' method returns a boolean value in Ruby on Rails?

user502052 picture user502052 · Feb 7, 2011 · Viewed 18.2k times · Source

I am using Ruby on Rails 3 and I would like to know what type of return will have the following code:

@user.destroy

I need that to handle cases on success and fault in someway like this:

if @user.destroy
  puts "True"
else
  puts "false"
end

Is it possible? If so, how?

Answer

idlefingers picture idlefingers · Feb 7, 2011

You should try what you're asking before asking. What you've got there will work just fine.

If the destroy works, it will return the object (which will pass as true in an if statement) and if not, it will return false or raise an exception, depending on why it failed.