On Wed, Feb 28, 2007 at 11:30:06AM +0100, Peter Bex wrote:
> It gets weirder.  I can ask Dependencies if the Employee class is loaded
> with Dependencies.send(:qualified_const_defined?, "Employee") and it says it
> isn't.  If I then ask for current_user.employees[0].class.id and for
> Employee.id, then it returns different values!
> 
> It looks like the Rails unloading mechanism doesn't handle dependencies
> right in combination with different load paths or with engines, somehow.
> I'll continue my investigations.  This might be related to the 
> 'lib folders and application controllers' thread on engines-users.

Problem solved!  In my model, I used:

require RAILS_ROOT + "/vendor/plugins/myplugin/app/models/user"

class User < ActiveRecord::Base
end

This means Rails does not mark the User class as unloadable, which means it
does not get unloaded on the next request.  Some classes that User depends
on (like Employee), do get reloaded, meaning the class that the old User
pointed to is some kind of "empty shell", a class which doesn't exist
anymore.  The new Employee class has all the right functions, but the old one
that User still points to doesn't.

At least, that's my understanding of how it works.  The solution was

require_dependency RAILS_ROOT + "/vendor/plugins/myplugin/app/models/user"

class User < ActiveRecord::Base
end

James, could you please add some kind of warning to the engines
documentation that any classes you use in an engine have to be loaded
with require_dependency instead of require, because otherwise it does
weird stuff?

Regards,
Peter Bex
Solide ICT - http://www.solide-ict.nl
_______________________________________________
engine-developers mailing list
[email protected]
http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org

Reply via email to