# Inheritance problem in model

**URL:** <https://www.ruby-forum.com/t/inheritance-problem-in-model/115865>\
**Category:** Rails\
**Created:** [September 29, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865 "2007-09-29T14:45:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 29, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865/1 "2007-09-29T14:45:14Z")

</div>

Hello,

I’ve got a problem with the inheritance as soon as I try to use it in a  
model.

I’ve got plenty of items which can share a lot of code, so I created a  
class “Item”, which inherits from ActiveRecord::Base, but because it’s  
not a “real item” but an interface, I set it abstract (writing  
abstract\_class? method).  
Then I have all the differents items I can use (for instance:  
book,picture,…) which have their own class.

I tried to write it down like :

class Item \< ActiveRecord::base  
def abstract\_class?  
true  
end

#stuff  
end

class Book \< Item  
def abstract\_class?  
false  
end  
#stuff  
end

The problem is, when I call Book.new.title I get an error saying that  
the method doesn’t exist (even if Book table has got a title column),  
and if I try Book.find(:first).title, it works…  
I’ve got the impression that Book.new returns an Item instead of a Book  
… Does anyone know how to fix my problem?  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 29, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865/2 "2007-09-29T17:08:18Z")

</div>

You’ve defined abstract\_class? as an instance method rather than as a  
class method. It’s a little less verbose to just do  
class Item \< ActiveRecord::Base  
self.abstract\_class = true  
end

Fred

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 29, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865/3 "2007-09-29T18:05:32Z")

</div>

Hi,

I tryed that but the problem is still the same…

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 29, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865/4 "2007-09-29T19:01:07Z")

</div>

What’s the actual code you’ve got, the actual error message you get  
and the database structure of the books table ?

Fred

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 29, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865/5 "2007-09-29T20:10:14Z")

</div>

The code :

class Book \< Item

has\_and\_belongs\_to\_many :collections

validates\_presence\_of :label  
validates\_presence\_of :description

validates\_uniqueness\_of :label, :case\_sensitive =\> false

include Element

UPLOAD\_DIR=“uploads/”

self.abstract\_class=false

#methods  
end

class Item \< ActiveRecord::Base  
attr\_reader :product, :quantity

self.abstract\_class=true

def initialize  
@quantity = 1  
end

def increment\_quantity  
@quantity += 1  
end

def real\_price  
price \* @quantity  
end  
end

The error:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.include?

Extracted source (around line #5):

2: \<%=debug @element%\>  
3: \<% fieldset\_tag ‘Book data’ do %\>  
4:  
5: \<%=@form.text\_field :label%\>  
  
6: \<%=@form.text\_area :description%\>  
  
7: \<%=@form.file\_field :upload\_file%\>  
8: \<%end%\>

The error :  
#{RAILS\_ROOT}/vendor/rails/activerecord/lib/active\_record/base.rb:1844:in  
`method\_missing’  
…

SQL query creating the Book table

CREATE TABLE `books` (  
`id` int(11) NOT NULL auto\_increment,  
`label` varchar(255) default NULL,  
`description` text,  
`required_credits` int(11) default NULL,  
`url` varchar(255) default NULL,  
`price` int(11) NOT NULL,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB AUTO\_INCREMENT=33 DEFAULT CHARSET=latin1

If you need any other detail please let me know

thanks a lot

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 29, 2007, 8:33pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865/6 "2007-09-29T20:33:05Z")

</div>

At the moment I don’t have any initialize method … I used the default  
constructor… I tried to specify it calling super inside but still got  
the same error 😕

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 29, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865/7 "2007-09-29T20:20:09Z")

</div>

In your initialize you need to call the super class initialize method  
(AR::Base’s initialize sets up some important stuff). Also I seem to  
recall that there are some issues involved with overriding AR::Base’s  
implementation of initialize. At the very least you need to make your  
initialize pass through any arguments or block specified to super

Fred

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 29, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865/8 "2007-09-29T21:14:22Z")

</div>

Thanks a lot!

That solved the problem.

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 29, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/inheritance-problem-in-model/115865/9 "2007-09-29T20:59:27Z")

</div>

Sure you do:

def initialize  
@quantity = 1  
end

That should probably be

def initialize(\*args, &block)  
super(\*args, &block)  
@quantity = 1  
end

Or, don’t define initialize at all and use an after\_initialize  
callback.

Fred
