--
You received this message because you are subscribed to the Google Groups "Hobo Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/hobousers/-/0Awm6SDGCM8J.
To post to this group, send email to hobo...@googlegroups.com.
To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.
Rails 3 added the :restrict option for dependent associations - to use it:
has_many :foos, :dependent => :restrict
Attempting to delete a model that still has associated foos will raise an ActiveRecord::DeleteRestrictionError with the message:
"Cannot delete record because of dependent #{reflection.name}"
You'll need to catch this exception (probably in your controller) and figure out how to present it to the user.
DB-level constraints are certainly another option - note that you may need to change the default schema format to :sql to be able to test them. You'll still have to catch the exception, but it will be a database-specific one.
Finally, you can prevent users *seeing* the delete option by adding code like this to the model:
def destroy_permitted?
foos.empty?
end
This will remove Rapid-generated delete buttons from your interface, and attempting to delete a record will raise a PermissionDeniedError.
--Matt Jones