When I submit Style create form I would like to create a new Style with a few Features. The features are chosen from a multi-select box which is populated from a features table in the database. The through table should update appropriately, depending on the features chosen from the multi-select.
I am able to do the following within rails console:
@style = Style.first
@style.feature_ids = ['','2','4','6']
After typing the above I will see my through table updated with three rows the appropriate style_id and feature_id.
In my Style create form I have the following:
<%= m.input :feature_ids, :label => "Features", :collection => @features, :input_html => { :multiple => true } %>
On submission of the form I receive the following PG error:
PG::Error: ERROR: column "feature_id" is of type integer but expression is of type character varying at character 96
HINT: You will need to rewrite or cast the expression.
: INSERT INTO "stylefeatures" ("created_at", "feature_id", "style_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"
From the params hash:
{"utf8"=>"✓",
"authenticity_token"=>"lkdI9jhlhPW1P2Tyb8jFMKFM/kVXvgcJfSkL0qNH7xk=",
"style"=>{"name"=>"123",
"feature_ids"=>["",
"2",
"4",
"6"],
"commit"=>"Create Style"}
Your
@featuresis a collection of Strings, change it to integer and you’ll be good (that’s what your db’s complaining about).