# How to delete record from two table

**URL:** <https://www.ruby-forum.com/t/how-to-delete-record-from-two-table/173480>\
**Category:** Rails\
**Created:** [August 10, 2009, 11:23am UTC](https://www.ruby-forum.com/t/how-to-delete-record-from-two-table/173480 "2009-08-10T11:23:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 10, 2009, 11:23am UTC](https://www.ruby-forum.com/t/how-to-delete-record-from-two-table/173480/1 "2009-08-10T11:23:14Z")

</div>

hello all,

i got error following when i try to delete record from services table. i  
am display data from tables services and users so that it make  
“services\_users”  
logical table so that how to possible delete record from that table.

ActiveRecord::StatementInvalid in ServicesController#destroy

Mysql::Error: Table ‘notice\_development.services\_users’ doesn’t exist:  
SHOW FIELDS FROM `services_users`

RAILS\_ROOT: C:/ruby/addon/notice  
Application Trace | Framework Trace | Full Trace

# c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/connection\_adapters/abstract\_adapter.rb:212:in `log' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/connection_adapters/mysql_adapter.rb:320:in`execute’ c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/connection\_adapters/mysql\_adapter.rb:466:in `columns' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/reflection.rb:207:in`columns’ c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/associations/has\_and\_belongs\_to\_many\_association.rb:13:in `columns' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/has_and_belongs_to_many_association.rb:112:in`finding\_with\_ambiguous\_select?’ c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/associations/has\_and\_belongs\_to\_many\_association.rb:23:in `construct_find_options!' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_collection.rb:54:in`find’ c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/associations/association\_collection.rb:399:in `find_target' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_collection.rb:353:in`load\_target’ c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/associations/association\_collection.rb:287:in `length' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_collection.rb:220:in`clear’ (eval):3:in `destroy\_without\_callbacks’

Thanks  
wap

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 10, 2009, 1:34pm UTC](https://www.ruby-forum.com/t/how-to-delete-record-from-two-table/173480/2 "2009-08-10T13:34:57Z")

</div>

Hi  
If what I understood from your description right, you have to check  
:dependent =\> :destroy

Assuming your relationship is has\_many :through you can do like in  
model

Service like  
has\_many :services\_users, :dependent =\> :destroy  
has\_many :users, :through =\> :services\_users

similary in model User

Then for example when you delete a service all its corresponding entres  
from junction table services\_users will be deleted and viceversa Please  
check here

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

Sijo

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 10, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/how-to-delete-record-from-two-table/173480/3 "2009-08-10T14:44:25Z")

</div>

Sijo Kg wrote:

> Hi  
> If what I understood from your description right, you have to check  
> :dependent =\> :destroy
> 
> Assuming your relationship is has\_many :through you can do like in  
> model
> 
> Service like  
> has\_many :services\_users, :dependent =\> :destroy  
> has\_many :users, :through =\> :services\_users
> 
> similary in model User
> 
> Then for example when you delete a service all its corresponding entres  
> from junction table services\_users will be deleted and viceversa Please  
> check here
> 
> [ActiveRecord::Associations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)
> 
> Sijo  
> uninitialized constant Service::ServicesUser  
> i got this error as per your suggestion

i want to delete only from services table record not in users

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 10, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/how-to-delete-record-from-two-table/173480/4 "2009-08-10T14:50:52Z")

</div>

On Aug 10, 1:44 pm, Wap A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> uninitialized constant Service::ServicesUser  
> i got this error as per your suggestion
> 
> i want to delete only from services table record not in users

Do you have a ServicesUser join model between services and users ?  
From your first post it looks like you certainly don’t have a  
services\_users table. If you are only trying to delete a row from the  
services table, what is wrong with Service.find(some\_id).destroy ?

Fred

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 10, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/how-to-delete-record-from-two-table/173480/5 "2009-08-10T15:03:20Z")

</div>

Frederick C. wrote:

> On Aug 10, 1:44ï¿½pm, Wap A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > uninitialized constant Service::ServicesUser  
> > i got this error as per your suggestion
> > 
> > i want to delete only from services table record not in users
> 
> Do you have a ServicesUser join model between services and users ?  
> From your first post it looks like you certainly don’t have a  
> services\_users table. If you are only trying to delete a row from the  
> services table, what is wrong with Service.find(some\_id).destroy ?
> 
> Fred

i have no services\_users table and ServicesUser model in my  
service.rb  
has\_many :services\_users, :dependent =\> :destroy  
has\_many :users, :through =\> :services\_users  
user.rb  
has\_many :services\_users, :dependent =\> :destroy  
has\_many :services, :through =\> :services\_users  
services\_controller.rb  
@services = Service.find(:all, :select =\>  
‘services.id,services.title,services.no\_or,services.price,users.FirstName,services.created\_at’,:joins=\>  
‘left join users on services.user\_id = users.id’)

i am using this code and access data from both table but when i try to  
delete record from service i got error serviceuser

\<% @services.each do |service| %\>

\<%=h service.title %\> \<%=h service.price %\> \<%=h service.FirstName%\> \<% t = service.created\_at %\> \<%=h t.strftime('%d-%m-%Y, %H:%M')%\> \<%= link\_to 'Destroy',service, :action =\> 'destroy', :confirm =\> 'Are you sure?',:method =\> :delete %\> \<%end%\>

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 11, 2009, 5:51am UTC](https://www.ruby-forum.com/t/how-to-delete-record-from-two-table/173480/6 "2009-08-11T05:51:21Z")

</div>

Hi  
To get a good understanding first read these

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

Sijo

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 11, 2009, 7:53am UTC](https://www.ruby-forum.com/t/how-to-delete-record-from-two-table/173480/7 "2009-08-11T07:53:37Z")

</div>

Sijo Kg wrote:

> Hi  
> To get a good understanding first read these
> 
> [Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association)
> 
> [Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association)
> 
> Sijo

thanks guy i can done it thanks
