我有两个模型,Story和Category。使用表格Story#new
,我希望能够保存类别的外键。在Rails窗体中合并多个模型以创建一个新对象
要显示Story#new
窗体页上从类别选择数据我用:
<%= collection_select(:category , :category, Category.all , :id, :category, {:prompt => 'Select Category...'}) %>
如何节省category_id
到新创建的故事对象?
故事属性:industry_id
和user_id
,类别有属性name
development.log告诉我这一点:(它看起来像它的尝试一个新的类别(99)添加到分类表
Started POST "/stories" for 127.0.0.1 at 2012-02-10 17:32:56 -0600
Processing by StoriesController#create as HTML
story"=>{"industry_id"=>"8", "user_id"=>"8"}, "category"=>{"category"=>"99"}, "commit"=>"Create Story"}
故事'belongs_to'类别?协会是如何制定的? – Substantial 2012-02-11 05:44:24
烨:'类故事<的ActiveRecord :: Base的 的has_many:行业 的has_many:categories'和'类分类<的ActiveRecord :: Base的 belongs_to的:story' – wantrapreneur 2012-02-12 03:26:05
所以呀其实故事的has_many类别 – wantrapreneur 2012-02-12 03:27:03