# Problem with assocations

**URL:** <https://www.ruby-forum.com/t/problem-with-assocations/86867>\
**Category:** Engines Development\
**Created:** [February 26, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/problem-with-assocations/86867 "2007-02-26T16:17:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 26, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/problem-with-assocations/86867/1 "2007-02-26T16:17:00Z")

</div>

Hi there,

We’re having some problems with some code in our Engine:

This code, in our _application’s_ (not the engine’s) user\_controller  
goes wrong:  
current\_company = current\_user.employees[0].company  
it raises an exception that the method ‘company’ does not exist for  
the instance of Employee current\_user.employees[0].

Employee is defined in the application itself, and User is defined in  
the engine. Employee belongs\_to :company and Company has\_many  
:employees.

This works fine:  
current\_company = Company.find(current\_user.employees[0].company\_id)

To check it’s not because the user object is instantiated in code from  
our  
engine, I tried this:  
current\_company = User.find(current\_user.id).employees[0].company  
This didn’t work either. Gives the same error (unknown method company  
for  
Employee)

Any ideas what could cause this? (or if it could be engines-related at  
all)

Regards,  
Peter B.  
Solide ICT - [http://www.solide-ict.nl](http://www.solide-ict.nl)

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 26, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/problem-with-assocations/86867/2 "2007-02-26T16:35:02Z")

</div>

The engines plugin doesn’t do any messing with ActiveRecord’s  
associations - so long as your classes are both loaded, I can’t see  
any reason why this would result from having the engines plugin  
installed…

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 28, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/problem-with-assocations/86867/3 "2007-02-28T16:20:58Z")

</div>

On Wed, Feb 28, 2007 at 11:30:06AM +0100, Peter B. 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 B.  
Solide ICT - [http://www.solide-ict.nl](http://www.solide-ict.nl)

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 28, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/problem-with-assocations/86867/4 "2007-02-28T17:39:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 28, 2007, 11:29am UTC](https://www.ruby-forum.com/t/problem-with-assocations/86867/5 "2007-02-28T11:29:52Z")

</div>

On Mon, Feb 26, 2007 at 04:15:45PM +0100, Peter B. wrote:

> Employee is defined in the application itself, and User is defined in  
> the engine. Employee belongs\_to :company and Company has\_many :employees.
> 
> This works fine:  
> current\_company = Company.find(current\_user.employees[0].company\_id)

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.

Regards,  
Peter B.  
Solide ICT - [http://www.solide-ict.nl](http://www.solide-ict.nl)
