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:inexecute’
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:incolumns’
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:infinding_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:infind’
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:inload_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:inclear’
(eval):3:in `destroy_without_callbacks’

Thanks
wap

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

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

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

On Aug 10, 1:44 pm, Wap A. [email protected]
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

Frederick C. wrote:

On Aug 10, 1:44�pm, Wap A. [email protected]
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%>

Hi
To get a good understanding first read these

Sijo

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