I have a database of movies and their associated genres.
class Genre < ActiveRecord::Base
has_many :movie_genres
has_many :movies, through: :movie_genres, dependent: delete_all
before_destroy :delete_movie
private
def delete_movie
self.movies.delete_all
end
end
class Movie < ActiveRecord::Base
has_many :movie_genres
has_many :genres, through: :movie_genres
end
My aim is that when a genre gets deletes - all of its movies also get deleted, regardless of if that movie has another genre or not. I've tried attaching dependent: :destroy to the Genre model however only the join table records get deleted. I've also tried using the before_destroy callback to delete the movies prior to deleting the genre however that didn't seem to work either.