我有关系:Post
一对多Pictures
。Pictures
之前已通过 AJAX 创建,并且它们的 ID 存储在一个帖子视图页面的隐藏字段中。提交表单后,仅保存自己的数据并从嵌套模型中post_controller
跳过。picture_ids
如何关联?
def create
p_attr = params[:post]
p_attr[:picture_ids] = ActiveSupport::JSON.decode(params[:post][:picture_ids])
puts p_attr
@post = Post.new(p_attr)
puts @post.to_json
@post.save
end
收到的帖子:
{"title"=>"test", "description"=>"test", "picture_ids"=>["505eec681e7bf2b8150001a5","505eea991e7bf2b8150001a1","505ee7761e7bf2b81500018d"]}
创建的帖子:
{"_id":"505eeed01e7bf2b8150001b1","created_at":null,"description":"test","title":"test","updated_at":null}