Active Record Nested Attributesの記事の抜粋です。 accepts_nested_attributes_forは、親子関係のある関連モデル(Project has_many :tasks や Enquate has_many :questionsなど)で、親から子を作成したり保存したりするときに使える。 1対1の時 class Member < ActiveRecord::Base has_one :avatar accepts_nested_attributes_for :avatar end params = { member: { name: 'Jack', avatar_attributes: { icon: 'smiling' } } } member = Member.create(params[:member]) member.avatar.id
![複数の子レコードを作成・更新する. accepts_nested_attributes_for - Qiita](https://cdn-ak-scissors.b.st-hatena.com/image/square/c2b6deffd9b8fac306c108827b106e9194826426/height=288;version=1;width=512/https%3A%2F%2Fqiita-user-contents.imgix.net%2Fhttps%253A%252F%252Fcdn.qiita.com%252Fassets%252Fpublic%252Farticle-ogp-background-412672c5f0600ab9a64263b751f1bc81.png%3Fixlib%3Drb-4.0.0%26w%3D1200%26mark64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZ3PTk3MiZoPTM3OCZ0eHQ9JUU4JUE0JTg3JUU2JTk1JUIwJUUzJTgxJUFFJUU1JUFEJTkwJUUzJTgzJUFDJUUzJTgyJUIzJUUzJTgzJUJDJUUzJTgzJTg5JUUzJTgyJTkyJUU0JUJEJTlDJUU2JTg4JTkwJUUzJTgzJUJCJUU2JTlCJUI0JUU2JTk2JUIwJUUzJTgxJTk5JUUzJTgyJThCLiUyMGFjY2VwdHNfbmVzdGVkX2F0dHJpYnV0ZXNfZm9yJnR4dC1hbGlnbj1sZWZ0JTJDdG9wJnR4dC1jb2xvcj0lMjMyMTIxMjEmdHh0LWZvbnQ9SGlyYWdpbm8lMjBTYW5zJTIwVzYmdHh0LXNpemU9NTYmcz00Mjg0ZmE3MzEzMGVkNjAxMWJiZjg1ZjUxNTBjMmIxNQ%26mark-x%3D142%26mark-y%3D57%26blend64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZoPTc2Jnc9NzcwJnR4dD0lNDBobXVyb25ha2EmdHh0LWNvbG9yPSUyMzIxMjEyMSZ0eHQtZm9udD1IaXJhZ2lubyUyMFNhbnMlMjBXNiZ0eHQtc2l6ZT0zNiZ0eHQtYWxpZ249bGVmdCUyQ3RvcCZzPWEzNjllZTk3ZDExNTMwNzJlOWM4NzU2YjI5MDM1ZjZl%26blend-x%3D142%26blend-y%3D486%26blend-mode%3Dnormal%26s%3D360106bf7e775f7ab6c8e9e06e366033)