ActiveRecord :dependent fails for me

I have a simple has_many relationship between a Movie and its Previews. I've verified that the previews table has a foreign key to its parent record in the movies table. I've added the has_many with

The problem I'm seeing is that when I delete a movie, I am not seeing any attempt of error in trying to delete the child previews records. The movie is deleted from the database.

Environment: I am running Rails 3.1.1 with Ruby 1.9.2 and have tried both Postgresql and Sqlite3 as my databases on my local OS X environment. Any insight to this problem (potentially self inflicted) will be appreciated. Thanks in advance.

Some observations: I can instantiate the movie and get its previews as in: Movie.find(12).previews and that succeeds. I can also locate a preview and reference its parent movie as in Preview.find(100).movie and that succeeds.

Troubleshooting: I've tried using the :destroy_all and even :nullify but still dont' see any attempt to handle the dependent activity. I've modified my database.yml to use SQLite3 instead of Postgresql and that didn't seem to help.

My ActiveRecord model class definitions look like this: class Movie < ActiveRecord::Base   has_many(:previews, :dependent => :delete_all) end

class Preview < ActiveRecord::Base   belongs_to(:movie) end

I have a simple has_many relationship between a Movie and its Previews. I've verified that the previews table has a foreign key to its parent record in the movies table. I've added the has_many with a :dependent => :delete_all as an option on the has_many association.

The problem I'm seeing is that when I delete a movie, I am not seeing any attempt of error in trying to delete the child previews records. The movie is deleted from the database.

How are you deleting the movie?

Fred

Hi. I'm instantiating the movie via a finder, such as @movie = Movie.find(params[:id]) and then calling @move.delete. I've seen this issue with my running web application as well as any attempt using the rails console.

I haven't tried @movie.destroy because I don't have a before or after destroy callback defined.

Actually you do - it's how :dependent is implemented. 99.9% of the time you want to call destroy on AR objects, not delete.

--Matt Jones

This might be useful:

http://stackoverflow.com/questions/2797339/rails-dependent-destroy-vs-dependent-delete-all

> Hi. I'm instantiating the movie via a finder, such as @movie = > Movie.find(params[:id]) > and then calling @move.delete. I've seen this issue with my running > web application as well as any attempt using the rails console.

> I haven't tried @movie.destroy because I don't have a before or after > destroy callback defined.

Actually you do - it's how :dependentis implemented. 99.9% of the time you want to call destroy on AR objects, not delete.

--Matt Jones

Hi, Matt, I've confirmed your point that I must call AR#delete in order for the :dependent behavior to work. Thank you for the clarification and solution. It's much appreciated!