# Active record multiple table inheritance with abstract parent

**URL:** <https://www.ruby-forum.com/t/active-record-multiple-table-inheritance-with-abstract-parent/173453>\
**Category:** Rails\
**Created:** [August 9, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/active-record-multiple-table-inheritance-with-abstract-parent/173453 "2009-08-09T16:18:18Z")\
**Posts on this page:** 6\
**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 9, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/active-record-multiple-table-inheritance-with-abstract-parent/173453/1 "2009-08-09T16:18:18Z")

</div>

I have four tables: jp\_properties, cn\_properties, jp\_dictionaries and  
cn\_dictioanries.

And every jp\_property belongs to a jp\_dictionary with foreign key  
“dictionary\_id” in table.

Similarly, every cn\_property belongs to a cn\_dictionary with foreign  
key “dictionary\_id” in table too.

Since there are a lot of same functions in both property model and  
both dictionary model, I’d like to group all these functions in  
abstract\_class.

The Models are like this:

class Property \< AR::Base  
self.abstract\_class = true  
belongs\_to :dictionry,  
:foreign\_key=\>‘dictionary\_id’,  
:class=\> —ModelDomainName— + “Dictionary”

### functions shared by JpProperty and CnProperty

end

class Dictionary \< AR::Base  
self.abstract\_class = true  
has\_many :properties,  
:foreign\_key=\>‘dictionary\_id’,  
:class=\> —ModelDomainName— + “Dictionary”

### functions shared by JpDictionary and CnDictionary

end

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

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

class JpDictionary \< Dictionary  
:set\_table\_name :jp\_dictionaries  
end

class CnDictionary \< Dictionary  
:set\_table\_name :cn\_dictionaries  
end

As you can see from the above code, the —ModelDomainName— part is  
either ‘Jp’ or ‘Cn’. And I want to get these string dynamically from  
the instances of JpProperty, JpDictionary, CnProperty or CnDictionary.

For example:

tempJpProperty = JpProperty.first  
tempJpProperty.dictionary #=\> will get ‘Jp’ from tempJpProperty’s  
class name and then apply it to the “belongs\_to” declaration.

So the problem is I don’t know how to specify the —  
ModelDomainName— part.

More specifically, I have no idea how to get subclass’s instance  
object’s class name within the parent class’s body.

Can you please help me with this problem?

---

<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 9, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/active-record-multiple-table-inheritance-with-abstract-parent/173453/2 "2009-08-09T22:30:36Z")

</div>

boblu wrote:

> I have four tables: jp\_properties, cn\_properties, jp\_dictionaries and  
> cn\_dictioanries.
> 
> And every jp\_property belongs to a jp\_dictionary with foreign key  
> “dictionary\_id” in table.
> 
> Similarly, every cn\_property belongs to a cn\_dictionary with foreign  
> key “dictionary\_id” in table too.
> 
> Since there are a lot of same functions in both property model and  
> both dictionary model, I’d like to group all these functions in  
> abstract\_class.
> 
> The Models are like this:
> 
> class Property \< AR::Base  
> self.abstract\_class = true  
> belongs\_to :dictionry,  
> :foreign\_key=\>‘dictionary\_id’,  
> :class=\> —ModelDomainName— + “Dictionary”
> 
> ### functions shared by JpProperty and CnProperty
> 
> end
> 
> class Dictionary \< AR::Base  
> self.abstract\_class = true  
> has\_many :properties,  
> :foreign\_key=\>‘dictionary\_id’,  
> :class=\> —ModelDomainName— + “Dictionary”
> 
> ### functions shared by JpDictionary and CnDictionary
> 
> end
> 
> class JpProperty \< Property  
> :set\_table\_name :jp\_properties  
> end
> 
> class CnProperty \< Property  
> :set\_table\_name :cn\_properties  
> end
> 
> class JpDictionary \< Dictionary  
> :set\_table\_name :jp\_dictionaries  
> end
> 
> class CnDictionary \< Dictionary  
> :set\_table\_name :cn\_dictionaries  
> end
> 
> As you can see from the above code, the —ModelDomainName— part is  
> either ‘Jp’ or ‘Cn’. And I want to get these string dynamically from  
> the instances of JpProperty, JpDictionary, CnProperty or CnDictionary.
> 
> For example:
> 
> tempJpProperty = JpProperty.first  
> tempJpProperty.dictionary #=\> will get ‘Jp’ from tempJpProperty’s  
> class name and then apply it to the “belongs\_to” declaration.
> 
> So the problem is I don’t know how to specify the —  
> ModelDomainName— part.
> 
> More specifically, I have no idea how to get subclass’s instance  
> object’s class name within the parent class’s body.
> 
> Can you please help me with this problem?

This is untested but worth a try.

class Property \< AR::Base  
self.abstract\_class = true  
def self.inherited(klass)  
klass.to\_s =~ /(\w+)[A-Z].\*/  
language = $1  
klass.belongs\_to :dictionary, :foreign\_key =\> ‘dictionary\_id’,  
:class =\> “#{language}Dictionary”  
end  
end

class Dictionary \< AR::Base  
self.abstract\_class = true  
def self.inherited(klass)  
klass.to\_s =~ /(\w+)[A-Z].\*/  
language = $1  
klass.has\_many :properties, :foreign\_key =\> ‘dictionary\_id’,  
:class =\> “#{language}Property”  
end  
end

Basically you have to delay the creation of the association until the  
subclass is created.

Best,  
Michael G.

---

<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 10, 2009, 2:28am UTC](https://www.ruby-forum.com/t/active-record-multiple-table-inheritance-with-abstract-parent/173453/3 "2009-08-10T02:28:05Z")

</div>

Thanks a lot for your reply.

I just tried your suggestion, but I did not get luck.

This is the console output:

Jp::Property.first  
TypeError: can’t dup NilClass  
from /rails/activerecord/lib/active\_record/base.rb:2189:in `dup' from /rails/activerecord/lib/active_record/base.rb:2189:in `scoped\_methods’  
from /rails/activerecord/lib/active\_record/base.rb:2193:in  
`current_scoped_methods' from /rails/activerecord/lib/active_record/base.rb:2183:in `scope’  
from /rails/activerecord/lib/active\_record/base.rb:2414:in  
`set_readonly_option!' from /rails/activerecord/lib/active_record/base.rb:610:in `find’  
from /rails/activerecord/lib/active\_record/base.rb:623:in `first’  
from (irb):2

Is there any other way to 'delay the creation of the association until  
the subclass is created. '?

Thank you

On 8æœˆ10æ—¥, åˆå‰5:30, Michael G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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 10, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/active-record-multiple-table-inheritance-with-abstract-parent/173453/4 "2009-08-10T15:20:44Z")

</div>

anyboby any ideas?

---

<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 10, 2009, 4:09pm UTC](https://www.ruby-forum.com/t/active-record-multiple-table-inheritance-with-abstract-parent/173453/5 "2009-08-10T16:09:20Z")

</div>

To IIan

Can you tell me what do you mean?

If you mean whether I tried the following

class Property \< AR::Base  
self.abstract\_class = true  
belongs\_to :dictionary, :foreign\_key =\> ‘dictionary\_id’, :class =\> “#  
{self.class.name.to\_s[0…1]}Dictionary”  
end

I can tell you it does not work, because the ‘belongs\_to’ declaration  
will be evaluated in the abstract class and the sub class will only  
inherit the methods generated by ‘belongs\_to’.

On 8æœˆ10æ—¥, åˆå¾Œ10:27, Ilan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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 10, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/active-record-multiple-table-inheritance-with-abstract-parent/173453/6 "2009-08-10T15:27:04Z")

</div>

boblu wrote:

> anyboby any ideas?

self.class.name() doesn’t work in the super?
