Topic: Single Table Inheritance type column issues

I'm having problems identical to this post I found when searching for a solution.

To summarize: I'm using STI and trying to use collection_select in my form to create a dropdown of possible options for  the record's "type". The error I get reads:

"wrong argument type String (expected Module)"

The linked post and associated discussion suggests renaming the type column to something else. I'm just trying to learn the Rails "golden path" so I'd like to know what is the canonical way to use STI. Why was the column for STI named "type" if it clashes with ruby's Object#type?

Re: Single Table Inheritance type column issues

werdnativ wrote:

Why was the column for STI named "type" if it clashes with ruby's Object#type?

Very good question. I'm guessing they didn't realize it until it was too late. Now they don't want to change it because it won't be backwards compatible. I wouldn't be surprised if they fix this in 2.0 - but that is a ways away.

Changing the column name may be the best solution, but you can also do this to work around it (say the model is User):

# in view
<%= select_tag :user_type, options_for_select(['Admin', 'Member'], @user[:type]) %>

# in controller
@user = User.find(params[:id])
@user[:type] = params[:user_type]
if @user.update_attributes(params[:user])
  #...
end

Railscasts - Free Ruby on Rails Screencasts

Re: Single Table Inheritance type column issues

Thanks, I've gone with renaming the type column. I guess one call to set_inheritance_column is going to be tidier than your alternate solution in the long run.