# Single Table inheritance question

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-question/51411>\
**Category:** Rails\
**Created:** [December 30, 2005, 11:49pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-question/51411 "2005-12-30T23:49:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 30, 2005, 11:49pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-question/51411/1 "2005-12-30T23:49:17Z")

</div>

So, I have a model called Organization. Chapter and Committee inherit  
from it.

I’m letting the user create new Organizations. So, they were able to  
select whether or not the Organization was a Chapter or Committee.

When I tried to do stuff like \<%= radio\_button ‘organization’, 'type  
%\>, I got a ton of warning messages about how I should be using #class  
and not #type. And doing tests on the type field of an organization  
also reported a bunch of warnings.

I was able to fix the whole thing by using a ‘kind’ column (instead of  
‘type’) in the organizations table and using that as the inheritance  
column, but it seems weird that I had to do stuff like that.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 30, 2005, 11:55pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-question/51411/2 "2005-12-30T23:55:18Z")

</div>

On 12/30/05, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was able to fix the whole thing by using a ‘kind’ column (instead of  
> ‘type’) in the organizations table and using that as the inheritance  
> column, but it seems weird that I had to do stuff like that.

I’ve ended up doing the same. My base class is ‘Rule’, so I went with  
rule\_type as the inheritance column.

While we’re here, does anyone have a better suggestion than this?  
I’ve got a postback-style action called new\_rule, which contains a  
select box for the user to select which kind of ‘rule’ they’d like,  
etc, etc.  
Because it’s a postback, there are no ‘rule’ parameters the first time  
through. I’ve got something like this going, but I’m always paranoid  
about making things difficult for myself.

def new\_rule  
rule\_type = ‘Rule’  
if params[:rule]  
rule\_type = params[:rule][:rule\_type] || ‘Rule’  
end  
klass = Object.const\_get(rule\_type)  
@rule = klass.new(params[:rule])

if request.post? and @rule.save  
flash[:notice] = “#{@rule.class} was successfully created.”  
redirect\_to :action =\> ‘show\_rule’, :id =\> @rule  
end  
end

With this, @rule starts out as an instance of Rule, but ends up as  
something more specific, such as DomainRule, before being saved.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 31, 2005, 12:01am UTC](https://www.ruby-forum.com/t/single-table-inheritance-question/51411/3 "2005-12-31T00:01:18Z")

</div>

Hi, Joe,

this is adressed in “agile web development with rails” as a less obvious  
constraint of the rails model of sti. The attribute type is also a name  
of a ruby method. therefore you shouldn’t adress it directly but via the  
objects indexing interface, using (in your case): organization[:type] =  
your radio\_button parameter

regards  
jan

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 31, 2005, 12:07am UTC](https://www.ruby-forum.com/t/single-table-inheritance-question/51411/4 "2005-12-31T00:07:22Z")

</div>

said that (obj.type as a deprecated synonym for Object#class in ruby:  
[class Object - RDoc Documentation](http://www.ruby-doc.org/core/classes/Object.html#M001065)) your approach  
seems to me as a fine workaround.

regards  
Jan

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 31, 2005, 12:10am UTC](https://www.ruby-forum.com/t/single-table-inheritance-question/51411/5 "2005-12-31T00:10:24Z")

</div>

On 12/30/05, Jan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > also reported a bunch of warnings.  
> > objects indexing interface, using (in your case): organization[:type] =  
> > your radio\_button parameter

Hi, thanks for pointing that out.

But, doing \<%= radio\_button “organization”, “type” %\> was causing a  
ton of warning messages.

It’s not a big deal though. Anyone know why ‘type’ was chosen as  
opposed to ‘\_type’?

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 31, 2005, 12:04am UTC](https://www.ruby-forum.com/t/single-table-inheritance-question/51411/6 "2005-12-31T00:04:21Z")

</div>

On 12/30/05, Wilson B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I was able to fix the whole thing by using a ‘kind’ column (instead of  
> > ‘type’) in the organizations table and using that as the inheritance  
> > column, but it seems weird that I had to do stuff like that.
> 
> I’ve ended up doing the same. My base class is ‘Rule’, so I went with  
> rule\_type as the inheritance column.

Seems like that should be the default, instead of ‘type’. The default  
choice seems broken (unless I’m understanding it wrong).
