通过设计在has_many中保存外键id数组

时间:2012-05-22 09:21:38

标签: ruby-on-rails

当我提交样式创建表单时,我想创建一个具有一些功能的新样式。这些功能是从多选框中选择的,该框选自数据库中的功能表。 表应根据从多选中选择的功能进行适当更新。

我可以在rails控制台中执行以下操作:

@style = Style.first
@style.feature_ids = ['','2','4','6']

输入上述内容后,我会看到我的表更新了三行相应的style_id和feature_id。

在我的样式中创建表单我有以下内容:

<%= m.input :feature_ids, :label => "Features", :collection => @features, :input_html => { :multiple => true } %>

提交表格后,我收到以下PG错误:

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"

来自params hash:

{"utf8"=>"✓",
 "authenticity_token"=>"lkdI9jhlhPW1P2Tyb8jFMKFM/kVXvgcJfSkL0qNH7xk=",
 "style"=>{"name"=>"123",
 "feature_ids"=>["",
 "2",
 "4",
 "6"],
 "commit"=>"Create Style"}

1 个答案:

答案 0 :(得分:1)

你的@features是一个字符串的集合,将它改为整数,你会很好(这就是你的数据库所抱怨的)。

相关问题