Single Table inheritance question

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?

On 12/30/05, Joe Van D. [email protected] 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.

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

said that (obj.type as a deprecated synonym for Object#class in ruby:
class Object - RDoc Documentation) your approach
seems to me as a fine workaround.

regards
Jan

On 12/30/05, Jan P. [email protected] 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’?

On 12/30/05, Wilson B. [email protected] 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).