how to delete record from two table

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'

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

Sijo

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

Frederick Cheung 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| %>   <tr align="center">     <td><a href="#" onClick="window.open('services/<%=h service.id %>','show','width=800,height=200')"><%=h service.title %></a></td>     <td><%=h service.price %></td>   <td><%=h service.FirstName%></td>   <% t = service.created_at %>   <td><%=h t.strftime('%d-%m-%Y, %H:%M')%></td>

<td><%= link_to 'Destroy',service, :action => 'destroy', :confirm => 'Are you sure?',:method => :delete %></td> </tr>   <%end%>

Sijo Kg wrote:

Hi   To get a good understanding first read these

Active Record Associations — Ruby on Rails Guides

Active Record Associations — Ruby on Rails Guides

Sijo

thanks guy i can done it thanks