新手问题 (菜鸟提问) 怎么把一篇文章存储到两张表中?

givemeluck · September 03, 2015 · Last by givemeluck replied at September 03, 2015 · 1563 hits

假如:‘标题’,‘作者’,‘发布时间’等存到主表,文章的‘内容’保存到一张附表当中。当接收到表单当中的数据以后,是否要先在主表中插入一条记录,获取自增 id.将 id 与文章内容写入到附表当中,通过 id 将两张表关联起来。 在获取自增 id 那一步会不会因为有多条插入语句同时执行而出错?有没有更简单有效的做法?

has_one :content
accepts_nested_attributes_for :content, update_only: true

详见 accepts_nested_attributes_for api

You need to Sign in before reply, if you don't have an account, please Sign up first.