Destroy vs delete

what is the difference between destroy and delete in AR

I am not sure what they mean by

“Destroys the record with the given id by instantiating the object and
calling
destroyhttp://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000866(all
the callbacks are the triggered). If an array of ids is provided, all
of them are destroyed.”

in Peak Obsession

I am guessing destrou uses the relationships in your model file to
delete
the has_one and has_many relationships and delete only deletes the
current
object

is this true?

cheers dion

yes this is true destroy does find all the relations…and ‘destroys’
them…wooot

sweet, now I love this AR stuff

On 7/23/06, Dion H. [email protected] wrote:

yes this is true destroy does find all the relations…and ‘destroys’
them…wooot

sweet, now I love this AR stuff

You may need to give AR a prod if you want all relations destroyed. In
your
has_many/has_one/HABTM relationships you need to include :dependent =>
:destroy_all

Daniel ----- wrote:

On 7/23/06, Dion H. [email protected] wrote:

yes this is true destroy does find all the relations…and ‘destroys’
them…wooot

sweet, now I love this AR stuff

You may need to give AR a prod if you want all relations destroyed. In
your
has_many/has_one/HABTM relationships you need to include :dependent =>
:destroy_all

:dependent => :destroy_all
must be added on all of them like this?

has_many :products :dependent => :destroy_all
has_many :lists :dependent => :destroy_all
has_many :friends :dependent => :destroy_all
has_many :buytogethers :dependent => :destroy_all
has_many :comments :dependent => :destroy_all

Thanks

Um… Care to further explain that one? I’m a little fuzzy on the long
answer, which seems to me to contradict the short answer.

RSL

Short answer: Yes

Slightly longer answer: destroy will not automatically destroy
associated records. Rather than have Rails default to destroying your
data, it sensibly refrains from rampages of destruction and leaves the
owner of the data to decide its fate.

Steve B. wrote:

Basically, when you call ‘destroy’ on an object, it will not
automatically remove the associated records unless you specifically ask
it to in the ‘has_many’ declaration.

askegg’s long answer is quite poetic but a little complicated :0)

What he said :slight_smile:

You don’t want a framework to assume you want all related data to be
destroyed.

Gotcha!

Basically, when you call ‘destroy’ on an object, it will not
automatically remove the associated records unless you specifically ask
it to in the ‘has_many’ declaration.

askegg’s long answer is quite poetic but a little complicated :0)