This project is archived and is in readonly mode.

#2627 ✓committed
Brian Samson

has_many :dependent => :delete_all throws exception on models in modules

Reported by Brian Samson | May 9th, 2009 @ 07:03 AM | in 2.3.6

If a model that is declared inside a module is associated via a has_many to another model in the same module, and :dependent is set to :delete_all or :nullify, an exception is thrown stating that "(Module) is not missing Constant (class)"

This is because the method that sets up the before_destroy handler just uses a text representation of the model name instead of the actual model class. The example I've been using is this:

class Astronomy::SolarSystem < ActiveRecord::Base
  has_many :planets, :dependent => :delete_all
end
The before destroy handler ends up making a call to Planet.delete_all instead of Astronomy::Planet.delete_all. I am attaching a patch with a test case that show this (and a similar flaw with :dependent => :nullify) and a solution to the problem.

I discovered this bug in rails 2.1 and it is still present in edge rails. The patch is meant to be applied to edge.

As an aside, I don't really like my particular solution because of its reliance on 'eval.' I think it would be a better idea to change the dependent_conditions to be parameterized instead of a string that needs to be double-evaled, but I'm not sure that will play well will every database backend. If it's ok to change that conditions clause to a parameterized query, let me know and I'll update the patch.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages