John Carlin
2005-Apr-10 19:38 UTC
before_destroy callback / maintaining referential integrity
Greets all, I''m pullin my hair out over something that I thought would be a piece of cake (and probably is). I''m trying to prevent a record from being destroyed if it is still referenced in another table. If it is referenced, I just want to add a message to flash or errors. I have: class Arrival < ActiveRecord::Base belongs_to :vessel_type validates_length_of :vessel, :within => 1..64 end class VesselType < ActiveRecord::Base has_one :arrival validates_length_of :name, :within => 1..64 validates_uniqueness_of :name, :message => "already exists" before_destroy :check_in_use protected def check_in_use # insert many attempts here end end Any tips or insight are gratefully appreciated. Please let me know if I can provide additional information. Thanks, John
David Heinemeier Hansson
2005-Apr-10 19:43 UTC
Re: before_destroy callback / maintaining referential integrity
> I''m pullin my hair out over something that I thought would be a piece > of cake (and probably is). I''m trying to prevent a record from being > destroyed if it is still referenced in another table. If it is > referenced, I just want to add a message to flash or errors. I have:You need to raise an exception in an before_destroy filter to stop the destruction from happening. The validations doesn''t really have anything to do with that. Validations are only something that govern saves (create/update). -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://www.loudthinking.com/ -- Broadcasting Brain