# Single Table Inheritance - Basic

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-basic/166680>\
**Category:** Rails\
**Created:** [April 28, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-basic/166680 "2009-04-28T15:01:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 28, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-basic/166680/1 "2009-04-28T15:01:34Z")

</div>

Hi,

I am struggling with a simple Single table Inheritance example.

I have two models, generated as follows:  
script/generate scaffold Contact type:string created\_at:datetime  
updated\_at:datetime  
script/generate scaffold Person fname:string lname:string

I amended the Person model:

class Person \< Contact  
end

I then expected to be able to create a Person, with a fname, but this  
fails (the following is from script/console):-

> > @person = Person.new =\> #\<Person id: nil, type: “Person”, created\_at: nil, updated\_at: nil\>  
> > @person.fname = “niciliketo”  
> > NoMethodError: undefined method `fname=' for #<Person:0xb6e8e5fc> from /home/nic/site/crm/vendor/rails/activerecord/lib/ active_record/attribute_methods.rb:251:in`method\_missing’  
> > from (irb):2
> 
> >

The examples I can find are pretty terse, and don’t seem to explain  
why this isn’t working. Can anyone tell me why this happens?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 28, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-basic/166680/2 "2009-04-28T15:26:55Z")

</div>

Did you run the database migration?

2009/4/28 removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 28, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-basic/166680/3 "2009-04-28T15:39:17Z")

</div>

On Apr 28, 1:57 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> from (irb):2
> 
> ```
> 
> The examples I can find are pretty terse, and don’t seem to explain  
> why this isn’t working. Can anyone tell me why this happens?

Because that’s not quite what single table inheritance does. The  
parent class is Contact, so instances of Person (and all other  
subclasses of Contact) are rows in the contacts tables and thus have  
the attributes that you specified when you created the contact model  
(ie type and created\_at)

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 28, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-basic/166680/4 "2009-04-28T16:28:46Z")

</div>

Thanks Percy,

Got it. I guess the clue is in the SINGLE TABLE piece 🙂

Apologies for my misunderstanding, and thanks for putting me straight.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 28, 2009, 4:25pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-basic/166680/5 "2009-04-28T16:25:49Z")

</div>

Yes, I ran the db migrations after creating the scaffolding and  
changing the models.

rake db:drop:all;  
rake db:create:all;  
rake db:migrate;
