Neal L
2009-Mar-29 16:10 UTC
prevent an object from being destroyed if it has related records
This is a total noob question, but how do you prevent a record from being destroyed if it has any related records. In my case, I have a book model and a page model. A book has_many pages. How do I prevent book.destroy from destroying a book if it has associated page records? Is there a :dependent option that would do this? Do I need to do some sort of before destroy callback? Thanks! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Bloom
2009-Mar-29 16:30 UTC
Re: prevent an object from being destroyed if it has related records
You could always use inheritance. class Book < ActiveRecord::Base def destroy return super if page.empty? return false end end On Mar 29, 11:10 am, Neal L <neal.lo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is a total noob question, but how do you prevent a record from > being destroyed if it has any related records. In my case, I have a > book model and a page model. A book has_many pages. > > How do I prevent book.destroy from destroying a book if it has > associated page records? Is there a :dependent option that would do > this? Do I need to do some sort of before destroy callback? > > Thanks!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Fernando Perez
2009-Mar-29 17:18 UTC
Re: prevent an object from being destroyed if it has related records
Andrew Bloom wrote:> You could always use inheritance. > > class Book < ActiveRecord::Base > def destroy > return super if page.empty? > return false > end > endI think you can refactor it to: def destroy page.empty? || super end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fernando Perez
2009-Mar-29 17:23 UTC
Re: prevent an object from being destroyed if it has related records
> I think you can refactor it to: > > def destroy > page.empty? || super > endMy bad: page.empty? && super untested though :-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marnen Laibow-Koser
2009-Mar-30 04:58 UTC
Re: prevent an object from being destroyed if it has related records
Neal L wrote:> This is a total noob question, but how do you prevent a record from > being destroyed if it has any related records.The database layer can do this easily, so harness its power! Put a foreign key constraint in your DB with ON DELETE RESTRICT. The Railsy way to do this is with the excellent foreign_key_migrations plugin (see http://github.com/harukizaemon ) -- which you should probably be using in any case. It works with mySQL and PostgreSQL; I''m not sure about other database systems. Best, -- Marnen Laibow-Koser marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org http://www.marnen.org -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---