# Single table inheritance - HowTo?

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-howto/59891>\
**Category:** Rails\
**Created:** [January 6, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-howto/59891 "2006-01-06T23:01:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 6, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-howto/59891/1 "2006-01-06T23:01:13Z")

</div>

Hi,

As per another member’s suggestion, I’m trying to user STI. So I have  
this table:

CREATE TABLE `settings` (  
`id` int(11) unsigned NOT NULL auto\_increment,  
`type` char(40) NOT NULL default ‘General’,  
`name` char(40) NOT NULL default ‘’,  
`value` char(128) NOT NULL default ‘’,  
`updated_on` datetime default NULL,  
`comments` char(255) default NULL,  
PRIMARY KEY (`id`)  
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

And a row with a type = ‘General’, and name = ‘front-page’

Then I have a setting.rb model, and also general\_setting.rb and  
user\_setting.rb, where I only have:

class Setting \< Setting  
end

So, if I do General.find\_by\_name(‘front-page’), I expected to get the  
value of the corresponding row, instead I get a nil object.

What could I possibly be doing wrong?

Thanks!

Ivan V.

---

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 7, 2006, 1:01am UTC](https://www.ruby-forum.com/t/single-table-inheritance-howto/59891/2 "2006-01-07T01:01:40Z")

</div>

[Note: HowTo in the subject suggests you’re providing a HowTo. You’re  
not.  
You’re asking a question.]

On Fri, Jan 06, 2006 at 03:59:18PM -0600, IvÃ¡n Vega R. wrote:

> PRIMARY KEY (`id`)  
> So, if I do General.find\_by\_name(‘front-page’), I expected to get the  
> value of the corresponding row, instead I get a nil object.

The value of the type field should be the name of the subclass you wish  
to  
instantiate for that row. So if your class is named GeneralSetting, you  
should be inserting type=‘GeneralSetting’.

- Matt

---

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/single-table-inheritance-howto/59891/3 "2006-01-07T11:08:27Z")

</div>

Iván

The ‘type’ column is used by Rails in the background and, most of the  
time, there is no need for you to manipulate it explicitly.

So, if you have  
class Foo \< ActiveRecord  
and  
class SuperFoo \< Foo

, you can query them directly:

```
allRecords = Foo.find(:all)
onlyTheSuperFoos = SuperFoo.find(:all)

```

Did that answer your question?

Alain

---

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 7, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-howto/59891/4 "2006-01-07T18:42:32Z")

</div>

Matthew, although a bit crankyly ;-), had the answer. I wasn’t using the  
correct type name for each class.

Although you do make a point. I should’ve used your method to test how  
STI worked in the first place.

Thanks!

Ivan V.
