Single Table inheritance question


#1

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?


#2

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


#3

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


#4

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

regards
Jan


#5

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


#6

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