Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 525
user502052 Avatar asked Feb 07 '11 14:02

user502052


People also ask

What does destroy return?

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.

What is difference between destroy and delete in Rails?

Basically destroy runs any callbacks on the model while delete doesn't. Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can't be persisted).

How do you destroy in rails?

Rails delete operation using destroy methodBy using destroy, you can delete the record from rails as well as its other existing dependencies. So in the context of our rails application, if we delete a book record using the destroy function, the authors associated with the book will also be deleted.

What does dependent destroy mean rails?

If you set the :dependent option to: :destroy, when the object is destroyed, destroy will be called on its associated objects. :delete, when the object is destroyed, all its associated objects will be deleted directly from the database without calling their destroy method.


1 Answers

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.

like image 73
idlefingers Avatar answered Sep 19 '22 07:09

idlefingers