swaminadhan
Joined: 14 Sep 2009 Posts: 54
|
Posted: Thu Dec 31, 2009 9:45 am Post subject: Difference between delete and destroy in ActiveRecord |
|
|
hello friends!
To delete a particular record we will go for delete or destroy methods.But according to the names both looks very similar, but the functionality wise both are different. so lets check the difference between delete and destroy methods in Active Record.
Now first check what the delete method will do:
Delete method deletes the row with a primary key matching the id argument, using a SQL DELETE statement, and returns the number of rows deleted. Active Record objects are not instantiated, so the object‘s callbacks are not executed, including any :dependent association options or Observer methods.
You can delete multiple rows at once by passing an Array of ids.
It is much faster than the destroy method, skipping callbacks might bypass business logic in your application that makes the function to work fast.
example:
Delete a single row
user.delete(1)
Delete multiple rows
user.delete([2,3,4])
Destroy method:
The destroy method makes the SQL call to the database and destroys the row in the table that contains it. It does still allow you to manipulate the object in the application as long as it’s still in scope (i.e) the callbacks and
filters are allowed even after destroying the object.
example :
Destroy a single object
User.destroy(1)
Destroy multiple objects
users= [1,2,3]
User.destroy(users)
Thank you,
swaminadhan. |
|