# STI & collection\_select

**URL:** <https://www.ruby-forum.com/t/sti-collection-select/196767>\
**Category:** Rails\
**Created:** [July 30, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/sti-collection-select/196767 "2008-07-30T17:15:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![DJDJDJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/46a35a/32.png) [@DJDJDJ](https://www.ruby-forum.com/u/DJDJDJ)\
**Post date:** [July 30, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/sti-collection-select/196767/1 "2008-07-30T17:15:32Z")

</div>

I am not understanding how to utilize collection\_select with a single  
table inheritance model I have been given to use so I hope someone can  
tell me what I am not thinking of.

Suppose there is a look-up table called Color (MySQL syntax):

CREATE TABLE Color  
( id INT NOT NULL AUTO\_INCREMENT,  
CONSTRAINT pk\_Color PRIMARY KEY(id),  
INDEX USING BTREE(id),  
name VARCHAR(50) NOT NULL  
);

And there is a product, lets use Card:

CREATE TABLE Card  
( id INT NOT NULL AUTO\_INCREMENT,  
CONSTRAINT pk\_Card PRIMARY KEY(id),  
INDEX USING BTREE(id),  
description VARCHAR(100) NOT NULL  
);

The table employed in the STI scheme looks like:

CREATE TABLE CardColor  
( id INT NOT NULL AUTO\_INCREMENT,  
CONSTRAINT pk\_CardColor PRIMARY KEY(id),  
INDEX USING BTREE(id),  
type VARCHAR(20) NOT NULL,  
Card\_id INT NOT NULL,  
CONSTRAINT fk\_CardColor\_Card\_id FOREIGN KEY (Card\_id) REFERENCES  
Card(id),  
Color\_id INT NOT NULL,  
CONSTRAINT fk\_CardColor\_Color\_id FOREIGN KEY (Color\_id) REFERENCES  
Color(id)  
);

My ActiveRecord models look like:

class Color \< ActiveRecord::Base  
set\_table\_name “Color”  
validates\_presence\_of :name  
validates\_length\_of :name, :maximum =\> 50  
end

class CardColor \< ActiveRecord::Base  
set\_table\_name ‘CardColor’  
belongs\_to :card, :class\_name =\> ‘Card’  
belongs\_to :color, :class\_name =\> ‘Color’  
end

class InsideColor \< CardColor  
end

class OutsideColor \< CardColor  
end

class Card \< ActiveRecord::Base  
set\_table\_name “Card”  
has\_many :insideColors, :class\_name =\> ‘InsideColor’, :dependent  
=\> :delete\_all  
has\_many :outsideColors, :class\_name =\> ‘OutsideColor’, :dependent  
=\> :delete\_all

validates\_presence\_of :description  
validates\_length\_of :description, :maximum =\> 100  
end

I thought my new.html.erb would employ something like:

\<%= collection\_select(:card, :insideColor, Color.find(:all, :order  
=\> :name), :id, :name, { :selected =\> @card.insideColors.collect {|c|  
c.Color\_id}}, {:multiple=\>true, :size =\> “3”}) %\>

But that yields an error during submit of: InsideColor(#36756150)  
expected, got String(#20706320)

How do I go about creating InsideColor objects with the related  
selected Color.id’s and place them in the association?

Thank you in advance for your time!
