# Multilevel class inheritance not working as expected

**URL:** <https://www.ruby-forum.com/t/multilevel-class-inheritance-not-working-as-expected/128508>\
**Category:** Ruby\
**Created:** [February 8, 2008, 10:11am UTC](https://www.ruby-forum.com/t/multilevel-class-inheritance-not-working-as-expected/128508 "2008-02-08T10:11:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joergbattermann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@joergbattermann](https://www.ruby-forum.com/u/joergbattermann)\
**Post date:** [February 8, 2008, 10:11am UTC](https://www.ruby-forum.com/t/multilevel-class-inheritance-not-working-as-expected/128508/1 "2008-02-08T10:11:04Z")

</div>

Hello there,

I am having a problem inheriting an already inherited class. errr…  
sounds more complicated than it is… look:

class Item \< ActiveRecord::Base

# has a name and description field

end

class Reference \< Item

# has a something field

end

class UrlReference \< Reference  
#has an url field  
end

… now If I do make a u = UrlReference.new in the console for  
example, the ‘u’ instance does NOT have the url field… only the ones  
from Reference and Item…

How come? I guess that’s basic ruby… but how do I properly inherit  
from other classes which again inherit from other ones? Did I miss  
something here?

Cheers & thanks,  
-Joerg

---

<div class="post-metadata">

**Author:** ![joergbattermann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@joergbattermann](https://www.ruby-forum.com/u/joergbattermann)\
**Post date:** [February 8, 2008, 10:45am UTC](https://www.ruby-forum.com/t/multilevel-class-inheritance-not-working-as-expected/128508/2 "2008-02-08T10:45:45Z")

</div>

On Feb 8, 2008 3:10 AM, Jörg Battermann [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # has a something field
> 
> How come? I guess that’s basic ruby… but how do I properly inherit  
> from other classes which again inherit from other ones? Did I miss  
> something here?
> 
> Cheers & thanks,

Hmm, not sure. I think maybe the problem lies within your database  
schema or your use of “single table inheritance” with ActiveRecord.

Class inheritance in pure Ruby is easy…

class A  
def initialize; @a=nil; end  
end  
class B \< A  
def initialize; super; @b=nil; end  
end  
class C \< B  
def initialize; super; @c=nil; end  
end  
c = C.new  
puts c.inspect

=\> #\<C:0x2df0848 @c=nil, @a=nil, @b=nil\>

Todd

---

<div class="post-metadata">

**Author:** ![joergbattermann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@joergbattermann](https://www.ruby-forum.com/u/joergbattermann)\
**Post date:** [February 9, 2008, 12:30am UTC](https://www.ruby-forum.com/t/multilevel-class-inheritance-not-working-as-expected/128508/3 "2008-02-09T00:30:58Z")

</div>

On 2/8/08, Jörg Battermann [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # has a something field
> 
> How come? I guess that’s basic ruby… but how do I properly inherit  
> from other classes which again inherit from other ones? Did I miss  
> something here?

First, ActveRecord attributes are not Ruby attributes, they are  
dynamically obtained from the SQL table the class is connected to.

Second, in reality, in Ruby you don’t inherit instance variables  
either, only methods. In fact I just got done writing a rather long  
article about this subtlety.

[http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-variable-is-it-anyway](http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-variable-is-it-anyway)

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
