# Destroy data from 2 different tables

**URL:** <https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189>\
**Category:** Rails\
**Created:** [May 9, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189 "2008-05-09T20:10:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [May 9, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189/1 "2008-05-09T20:10:46Z")

</div>

I’m trying to delete an account but at the same time i’d like to  
delete the portals that belongs to the account.  
This is what i wrote but it’s not working.  
how can i deal with this problem?

I have this code inside the account controller in the “destroy” method  
def destroy  
@account = Account.find(params[:id])  
@portal = Portal.find(:all, :conditions =\> [‘account\_id = ?’,  
@account.id ])  
@portal.destroy  
@account.destroy  
end  
any ideas?

the error displays this:

NoMethodError in AccountsController#destroy

undefined method `destroy' for #<Array:0x47982a0> app/controllers/accounts_controller.rb:78:in`destroy’  
Request

Parameters:

{"\_method"=\>“delete”,  
“authenticity\_token”=\>“f8c32ed76159e1b51b0883ef72a4ab801b92d322”,  
“id”=\>“11”}

---

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [May 9, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189/2 "2008-05-09T22:43:02Z")

</div>

I’m guessing the error is coming on line:

@portal.destroy

Right? The reason why is that:

@portal = Portal.find(:all, :conditions =\> [‘account\_id = ?’,  
@account.id ])

returns an array, even if there is only one result. Check this out:

> > User.find(:all, :conditions =\> “id = 1”).class  
> > =\> Array

Only one result is returned but it is wrapped in an array. And class  
Array does not have a destroy method, hence the error. One simple way  
around this would be to do:

@portals = Portal.find(:all, :conditions =\> [‘account\_id = ?’,  
@account.id ])  
…  
@portals.each { |p| p.destroy }

HTH,

Matt

---

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [May 9, 2008, 10:45pm UTC](https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189/3 "2008-05-09T22:45:58Z")

</div>

On 9-May-08, at 4:42 PM, Matt W. wrote:

> returns an array, even if there is only one result. Check this out:  
> …
> 
> > how can i deal with this problem?  
> > any ideas?
> > 
> > {"\_method"=\>“delete”,  
> > “authenticity\_token”=\>“f8c32ed76159e1b51b0883ef72a4ab801b92d322”,  
> > “id”=\>“11”}

or @portals = Portal.find(:first, :conditions [‘account\_id = ?’,  
@account.id]) #which will return a single AR instance

which would be a problem if Account has\_many :accounts

J

---

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [May 9, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189/4 "2008-05-09T22:55:21Z")

</div>

And by :accounts I think Jodi means :portals 🙂

---

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [May 9, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189/5 "2008-05-09T23:29:56Z")

</div>

Hi there,

–Thanks for the help, i appreciate your help,

solution #1  
–first i tried:  
@portals.each { |p| p.destroy }  
–and it displayed an error:  
NoMethodError in AccountsController#destroy

```
  You have a nil object when you didn't expect it!
  You might have expected an instance of Array.
  The error occurred while evaluating nil.each

  RAILS_ROOT: E:/frameworks/railsapps/listing
  Application Trace | Framework Trace | Full Trace

  app/controllers/accounts_controller.rb:81:in `destroy'

```

– I also tried this :

solution #2  
class Portal \< ActiveRecord::Base  
belongs\_to :account  
end

```
  class Account < ActiveRecord::Base
    has_many :portals, :dependent => :destroy
  end

```

but it only deleted the account record,  
and then i tryed this because somebody suggested it and it worked

```
for portal in @portal
  portal.destroy
end

```

# the portals under the account id were deleted.

thanks for all the options that all of you gave me,  
they seem very logical and very interesting  
maybe there’s something that i’m doing wrong,  
do you have any idea why i would have this error for the solution #1  
NoMethodError in AccountsController#destroy  
or why it wouldn’t do the job on solution #2?

---

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [May 9, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189/6 "2008-05-09T23:01:17Z")

</div>

RoR\_Nb wrote:

> I’m trying to delete an account but at the same time i’d like to  
> delete the portals that belongs to the account.  
> This is what i wrote but it’s not working.  
> how can i deal with this problem?

Why not specify it in the models?

class Portal \< ActiveRecord::Base  
belongs\_to :account  
end

class Account \< ActiveRecord::Base  
has\_many :portals, :dependent =\> :destroy  
end

delete the Account, and the Portals go poof!

---

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [May 11, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189/7 "2008-05-11T21:30:09Z")

</div>

Use :dependent=\>:destroy only if you need to do some ‘clean up’  
processing in the child models. Destroy instantiates each of the  
models before deleting them. If you don’t need to do any processing  
then use the more db-efficient :dependent=:delete\_all

On May 11, 12:20 am, Ar Chron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [May 11, 2008, 6:20am UTC](https://www.ruby-forum.com/t/destroy-data-from-2-different-tables/137189/8 "2008-05-11T06:20:48Z")

</div>

RoR\_Nb wrote:

> – I also tried this :
> 
> solution #2  
> class Portal \< ActiveRecord::Base  
> belongs\_to :account  
> end
> 
> ```
> class Account < ActiveRecord::Base
> has_many :portals, :dependent => :destroy
> end
> 
> ```
> 
> but it only deleted the account record,

Curious…

does your Portal model include an account\_id?

I use the :dependent =\> :destroy, and can cascade deletes down several  
levels of models…
