Son of a gun!  I spent all day ripping my app apart to provide a test  
case (cause I couldn't figure out how to recreate it from scratch.   
I've been posting on a similar thread on the engine-user forum).   
With everything stripped out, removing a

   require 'user'

that was at the top of my controller fixed the problem.  Also  
changing it to require_dependency fixed it as well.  Peter, thanks  
for finding the fix!  Nice job.

James, I hope you will add this method of overriding an engine model  
into the readme/docs - I think it's a better solution than using mixins.

Cheers,
Brett

On Feb 28, 2007, at 8:42 PM, Peter Bex wrote:

> 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

_______________________________________________
engine-developers mailing list
[email protected]
http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org

Reply via email to