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!