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.

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Frederick Cheung at Nov 22, 2011 at 8:25 am

    On Nov 22, 1:43 am, RubyNewbie wrote:
    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
    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
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • RubyNewbie at Nov 22, 2011 at 9:41 am
    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.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Matt Jones at Nov 22, 2011 at 11:14 pm

    On Nov 22, 4:41 am, RubyNewbie wrote:
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Joseph Pawletko at Nov 24, 2011 at 12:22 pm
    This might be useful:
    http://stackoverflow.com/questions/2797339/rails-dependent-destroy-vs-dependent-delete-all

    On Tue, Nov 22, 2011 at 6:13 PM, Matt Jones wrote:


    On Nov 22, 4:41 am, RubyNewbie wrote:
    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

    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • RubyNewbie at Nov 26, 2011 at 2:05 am

    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!

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedNov 22, '11 at 1:43a
activeNov 26, '11 at 2:05a
posts6
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase