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 -- Posted via http://www.ruby-forum.com/.
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 http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Sijo -- Posted via http://www.ruby-forum.com/.
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 > > http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html > > > > Sijouninitialized constant Service::ServicesUser i got this error as per your suggestion i want to delete only from services table record not in users -- Posted via http://www.ruby-forum.com/.
On Aug 10, 1:44 pm, Wap Addon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > uninitialized constant Service::ServicesUser > i got this error as per your suggestion > > i want to delete only from services table record not in usersDo 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:> On Aug 10, 1:44�pm, Wap Addon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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 ? > > Fredi 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%> -- Posted via http://www.ruby-forum.com/.
Hi To get a good understanding first read these http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association Sijo -- Posted via http://www.ruby-forum.com/.
Sijo Kg wrote:> Hi > To get a good understanding first read these > > http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association > > http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association > > > Sijothanks guy i can done it thanks -- Posted via http://www.ruby-forum.com/.