# Destroy di un record

**URL:** <https://www.ruby-forum.com/t/destroy-di-un-record/180074>\
**Category:** IT\
**Created:** [December 10, 2009, 10:22pm UTC](https://www.ruby-forum.com/t/destroy-di-un-record/180074 "2009-12-10T22:22:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorenz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9d8465/32.png) [@lorenz](https://www.ruby-forum.com/u/lorenz)\
**Post date:** [December 10, 2009, 10:22pm UTC](https://www.ruby-forum.com/t/destroy-di-un-record/180074/1 "2009-12-10T22:22:11Z")

</div>

Salve a tutti, avrei un problema su come distruggere un record di una  
tabella esterna. Praticamente ho una relazione “uno a uno” fra le  
tabelle Album e Download e ho il seguente codice:

class Album \< ActiveRecord::Base  
has\_one :download, :dependent =\> :destroy

…  
end

class Download \< ActiveRecord::Base  
belongs\_to :album

…  
end

Teoricamente se distruggo l’album, si drovrebbe distruggere anche il  
relativo download giusto? PerÃ² non accade. Ho provato con il togliere  
“:dependent =\> :destroy” e aggiungere nel controller dell’album:

def destroy  
@album = Album.find(params[:id])  
Album.find(:first, :conditions =\> [“album\_id = ?”,  
params[:id]]).destroy  
@album.destroy

```
........

```

end

Anche cosÃ¬ facendo non mi cancella il record nella tabella Download,  
sapete darmi una dritta??  
Grazie mille

---

<div class="post-metadata">

**Author:** ![lorenz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9d8465/32.png) [@lorenz](https://www.ruby-forum.com/u/lorenz)\
**Post date:** [December 11, 2009, 9:07am UTC](https://www.ruby-forum.com/t/destroy-di-un-record/180074/2 "2009-12-11T09:07:20Z")

</div>

Premessa: non ho fatto prove con il codice e quindi la disamina  
successiva Ã¨ soggetta a bug anche grossi, tuttavia…

Lorenzo C. wrote:

> def destroy  
> @album = Album.find(params[:id])  
> Album.find(:first, :conditions =\> [“album\_id = ?”,  
> params[:id]]).destroy  
> @album.destroy
> 
> ```
> ........
> 
> ```
> 
> end

Mi pare strano che la tabella albums abbia un attributo album\_id.  
Rimanda a sÃ© stessa? Mi sarei atteso download\_id.  
Il problema in realtÃ si capisce guardando il codice dei due modelli.

Cito

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001834)**
>
> Active Record Associations Associations are a set of macro-like class methods for tying objects together through foreign keys.

> class Album \< ActiveRecord::Base  
> has\_one :download, :dependent =\> :destroy

has\_one: “This method should only be used if the other class contains  
the foreign key. If the current class contains the foreign key, then you  
should use belongs\_to instead.”

Quindi albums non conterrÃ nessuna foreign key: nÃ© album\_id (ovvio) nÃ©  
download\_id.

> …  
> end
> 
> class Download \< ActiveRecord::Base  
> belongs\_to :album

Quindi downloads dovrÃ avere un attributo album\_id

Se invece vuoi che albums abbia download\_id dovrai mettere

belongs\_to :download, :dependent =\> :destroy

in album.rb e

has\_one :album

in download.rb. Che sia l’album ad appartenere (belongs\_to) al download  
mi sembra poco naturale da leggere per cui lascerei le cose come sono  
nei modelli correggendo solo gli attributi nelle tabelle.

La dependent destroy probabilissimamente non funzionava perchÃ© attributi  
e associazioni erano in contraddizione. Una volta corretti nel  
controller dovrebbe bastare

@album = Album.find(params[:id])  
@album.destroy

Paolo

> …  
> end

---

<div class="post-metadata">

**Author:** ![lorenz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9d8465/32.png) [@lorenz](https://www.ruby-forum.com/u/lorenz)\
**Post date:** [December 14, 2009, 7:13pm UTC](https://www.ruby-forum.com/t/destroy-di-un-record/180074/3 "2009-12-14T19:13:19Z")

</div>

Chiedo scusa, infatti ho sbagliato a scrivere, in realtÃ avevo fatto:

Download.find(:first, :conditions =\> [“album\_id = ?”,  
params[:id]]).destroy

E anche cosÃ¬ non funzionava…comunque ho risolto il problema  
semplicemente con la  
:dependent =\> :destroy, non so come ma alla fine Ã¨ andato tutto a  
posto!! Grazie mille
