dependent: :destroy is not deleting dependencies from views

jxdx

Projects have many rooms. When I delete a project from the view, the associated rooms are not deleted. Rooms also have many products which should also be deleted when a project is deleted.

Project class

class Project < ActiveRecord::Base
  belongs_to :user

  has_many :rooms, dependent: :destroy
  has_many :products, through: :rooms
end

Projects Controller

class ProjectsController < ApplicationController
    def destroy
        @project = current_user.projects.find(params[:id])
        if @project.delete
            redirect_to user_projects_path(@project.user)
        end
    end
end

Rooms Controller

class RoomsController < ApplicationController
    def destroy
        @room = Room.find(params[:id])
        if @room.delete
            redirect_to root_path
         end
     end

The delete link in the projects show view.

 = link_to "Delete", project_room_path(room.project, room), method: :delete, data: { confirm: "Are you sure?" }, title: room.title, class: "btn btn-danger"
Drew Ogryzek

Try changing the the destroy action to call the destroy method on the instance variable rather than delete.

 class ProjectsController < ApplicationController
   def destroy
     @project = current_user.projects.find(params[:id])
     if @project.delete
       redirect_to user_projects_path(@project.user)
     end
   end
 end

According to the documentation on Active Record Associations, "has_many :through associations have records in join tables, as well as the associated records. So ... ... it is assumed that deletion on an association is about removing the link between the owner and the associated object(s), rather than necessarily the associated objects themselves. So with ... has_many :through, the join records will be deleted, but the associated records won't."

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Deleting an Entrybox from grid in Tkinter with destroy()

Rails Foreign Key violation deleting has_many relationships with dependent destroy

dependent destroy not working

Find and destroy dependent records

Laravel Destroy Option not deleting

maven dependencies copied from dependent project after zip assemby

How to use dependent: :destroy in rails?

How to test dependent: :destroy with RSpec?

dependent:destroy throwing error with postgresql

dependent is set to destroy, nullifies instead

Deleting Record does not destroy association

I have created a association in rails on a author(has_many) and book(belongs_to), Now upon deleting the author(dependent: :destroy), i get exeception

Does iOS destroy the views on rotation?

Are sbt library dependencies order dependent?

Rails :dependent => :destroy VS :dependent => :delete_all

Difference between dependent destroy vs dependent delete on Rails Active Record

Rails: why cannot delete dependent table if dependent: :destroy allowed?

Rails dependent destroy and really destroy an acts_aS_paranoid object

Rails: dependent: :destroy - How does this work?

How it works - `belongs_to :user, dependent: :destroy`

Rails 5 dependent: :destroy doesn't work

Handle dependent destroy via active jobs

rails dependent destroy looking for non existing table

Rails dependent: :destroy on acts-as-taggable-on

Rails polymorphic dependent: :destroy not working correctly

rails dependent: :destroy works with wrong ID value

How do I search all dependent views created from tables within a whole schema?

Eclipse Plugin dependent upon jar and therefore make a plugin from the dependency: what about its dependencies?

How do you get and use the dependent type from a type class with functional dependencies?