Senior & Expert RoR Developers Discussion Forum by Nyros Technologies

HIRE Ruby on Rails Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

Difference between delete and destroy in ActiveRecord

 
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> The Mechanics of Ruby programming
View previous topic :: View next topic  
Author Message
swaminadhan



Joined: 14 Sep 2009
Posts: 54

PostPosted: Thu Dec 31, 2009 9:45 am    Post subject: Difference between delete and destroy in ActiveRecord Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> The Mechanics of Ruby programming
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts About Modules,Mixins and Namespaces
0 swaminadhan 7 Sat Mar 20, 2010 12:01 pm
swaminadhan View latest post
No new posts Railsdav plugin
0 swaminadhan 9 Sat Mar 20, 2010 11:20 am
swaminadhan View latest post
No new posts Deploying the rails applicatin in Heroku
0 Divya 14 Fri Mar 19, 2010 12:23 pm
Divya View latest post
No new posts overridig existing methods
0 Raja 14 Fri Mar 19, 2010 11:28 am
Raja View latest post
No new posts Class Methods
0 Raja 11 Fri Mar 19, 2010 11:04 am
Raja View latest post
No new posts Objects methods
0 Raja 13 Fri Mar 19, 2010 10:50 am
Raja View latest post
No new posts Documentation
0 Anu 24 Thu Mar 18, 2010 4:01 am
Anu View latest post
No new posts File handling in ruby
0 Anu 31 Wed Mar 17, 2010 2:14 pm
Anu View latest post
No new posts sending an email from rb file
0 umamahesh_nyros 21 Wed Mar 17, 2010 6:13 am
umamahesh_nyros View latest post
No new posts difference bewteen render component and render partial
0 umamahesh_nyros 20 Wed Mar 17, 2010 6:06 am
umamahesh_nyros View latest post




Hire an expert Ruby on Rails developer / coder / programmer or development team from India now!!

Other Forums : PHP   ::   .Net   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About Ruby on Rails Experts   |   More