Multilevel class inheritance not working as expected

  • Thread starter Jörg Battermann
  • Start date
J

Jörg Battermann

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
 
T

Todd Benson

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 =3D 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,

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=3Dnil; end
end
class B < A
def initialize; super; @b=3Dnil; end
end
class C < B
def initialize; super; @c=3Dnil; end
end
c =3D C.new
puts c.inspect

=3D> #<C:0x2df0848 @c=3Dnil, @a=3Dnil, @b=3Dnil>

Todd
 
R

Rick DeNatale

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 =3D 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?

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


--=20
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top