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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 22, 1:43 am, RubyNewbie <calvinanhngu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 22, 4:41 am, RubyNewbie <calvinanhngu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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 <al2o3cr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Nov 22, 4:41 am, RubyNewbie <calvinanhngu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > 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 JonesHi, 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.