# Why associations get error when defined in abstract\_class model

**URL:** <https://www.ruby-forum.com/t/why-associations-get-error-when-defined-in-abstract-class-model/173411>\
**Category:** Rails\
**Created:** [August 8, 2009, 4:57am UTC](https://www.ruby-forum.com/t/why-associations-get-error-when-defined-in-abstract-class-model/173411 "2009-08-08T04:57:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [August 8, 2009, 4:57am UTC](https://www.ruby-forum.com/t/why-associations-get-error-when-defined-in-abstract-class-model/173411/1 "2009-08-08T04:57:05Z")

</div>

I have many models and want to share common functions among them in  
abstract\_class models as many as possible.

But why the associations specified in abstract\_class do not work?

For example:

class Property \< AR::Base  
self.abstract\_class = true

belongs\_to :dictionary, :foreign\_key=\>‘dictionary\_id’, :class\_name  
=\>"#{self.class.parent.name}::Dictionary"  
#############  
I did this instead of specify them in Jp::Property and Cn::Property  
seperately.  
In Jp::Property  
belongs\_to :dictionary, :foreign\_key=\>‘dictionary\_id’, :class\_name  
=\>“Jp::Dictionary”  
In Cn::Property  
belongs\_to :dictionary, :foreign\_key=\>‘dictionary\_id’, :class\_name  
=\>“Cn::Dictionary”  
#############

end

class Dictionary \< AR::Base  
self.abstract\_class = true  
end

class Jp::Property \< Property  
set\_table\_name :jp\_properties  
end

class Jp::Dictionary \< Dictioanry  
set\_table\_name :jp\_dictionaries  
end

class Cn::Property \< Property  
set\_table\_name :cn\_properties  
end

class Cn::Dictionary \< Dictioanry  
set\_table\_name :cn\_dictionaries  
end

when I fire up console when input this

temp = Jp::Property.first =\> first property record  
temp.dictionary

NameError: uninitialized constant Dictionary  
from /rails/activesupport/lib/active\_support/dependencies.rb:443:in  
`load_missing_constant' from /rails/activesupport/lib/active_support/dependencies.rb:80:in`const\_missing’  
from /rails/activesupport/lib/active\_support/dependencies.rb:92:in  
`const_missing' from /rails/activerecord/lib/active_record/base.rb:2204:in`compute\_type’  
from /rails/activesupport/lib/active\_support/core\_ext/kernel/  
reporting.rb:11:in `silence_warnings' from /rails/activerecord/lib/active_record/base.rb:2200:in`compute\_type’  
from /rails/activerecord/lib/active\_record/reflection.rb:156:in  
`send' from /rails/activerecord/lib/active_record/reflection.rb:156:in`klass’  
from /rails/activerecord/lib/active\_record/associations/  
belongs\_to\_association.rb:44:in `find_target' from /rails/activerecord/lib/active_record/associations/ association_proxy.rb:240:in`load\_target’  
from /rails/activerecord/lib/active\_record/associations/  
association\_proxy.rb:112:in `reload' from /rails/activerecord/lib/active_record/associations.rb:1230:in`dictionary’

Can you please explain this?

---

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [August 8, 2009, 6:24am UTC](https://www.ruby-forum.com/t/why-associations-get-error-when-defined-in-abstract-class-model/173411/2 "2009-08-08T06:24:53Z")

</div>

I think maybe I figured out why I’m wrong.

The ‘belongs\_to’ declaration in abstract class is actually evaluated  
before the abstract class is inherited.

And the sub class which is inherited from abstract class only got all  
the instance methods which were generated by the ‘belongs\_to’  
declaration in its parent.

So, in my example,

I can never get ‘Jp’ or ‘Cn’ strings in the abstract class Property’s  
‘belongs\_to’ declaration because the instance object ‘temp’ never saw  
that declaration.

Please fix me if I am wrong.
