Project

General

Profile

Deleting records along with project

Added by Hubert Alejandro over 9 years ago

New to Redmine and Ruby/Rails in general, and cant seem to find the answer to my issue. I'm writing a plugin that has a single model. I would like records of this model to be associated with separate projects. When those projects are deleted, so should the associated records. I've added belongs_to :project to the model class, as well as adding t.belongs_to :project to the database migration, but I am not getting the desired effect. Can anyone please shed some light on this? Thanks!


Replies (1)

RE: Deleting records along with project - Added by Martin Denizet (redmine.org team member) over 9 years ago

Hello Hubert,
I you need to patch the Project model by doing something such as:

plugins/my_plugin/init.rb:

require 'redmine'

Rails.configuration.to_prepare do
     require_dependency 'my_plugin/patches/project_patch'
end
[...]

plugins/my_plugin/lib/my_plugin/patches/project_patch.rb:

module MyPlugin
  module Patches
    module ProjectPatch
      def self.included(base) # :nodoc:

        base.send(:include, InstanceMethods)

        base.class_eval do
          unloadable # Send unloadable so it will not be unloaded in development
          has_many :your_model_plural_here, :dependent => :delete_all
          #If you need your code to run the destroy method, use ":dependent => :destroy" instead
        end
      end

      module InstanceMethods
          #Add methods to the Project class here
      end
    end
  end
end
Project.send(:include, MyPlugin::Patches::ProjectPatch)

This is a dynamic/clean patch of Project. It's always a bad idea to edit Redmine's core files.
Note that you need to restart your web server for the patch to be applied, even in development environment.
Tell me how it goes.
Cheers,

    (1-1/1)