Rails：親レコードと一緒に複数の子レコードをINSERTする

#環境
Ruby 2.4.4
Rails 5.1.6

#やりたいこと
1つのプロジェクトに8名のメンバーが所属する。
新規プロジェクトの作成時に、1つのページ(view)でメンバーも登録させる。

#準備
##Model
projectテーブル：id, project_name
memberテーブル：id, member_name, project_id


```project.rb
class Project < ApplicationRecord
  has_many :members
  accepts_nested_attributes_for :members
end
```

```member.rb
class Member < ApplicationRecord
  belongs_to :project
end
```

##Controller
```projects_controller.rb
def new
  @project = Project.new
  @project.members.build
end

def create
  @project = Project.new(project_params)
  if @project.save
    redirect_to @project # 成功時
  else
    render 'new' # 失敗
  end
end

private
  def project_params
    params.require(:project).permit(
     :project_name, members_attributes:[:member_name, :project_id]
    )
  end
```

##View
```erb:new.html.erb
<%= form_for(@project) do |f| %>
  <%= f.label "プロジェクト名：" %>
  <%= f.text_field :project_name %>
    <% 8.times do %>
      <%= f.fields_for :members do |member| %>
        <%= member.text_field 'member_name' %>
      <% end %>
    <% end %>
  <%= f.submit "登録" %>
<% end %>
```

#結果
new.html(メンバー名入力の箇所のみ)

```new.html
<input type="text" name="project[members_attributes][0][member_name]" id="project_members_attributes_0_member_name">
<input type="text" name="project[members_attributes][1][member_name]" id="project_members_attributes_1_member_name">
<input type="text" name="project[members_attributes][2][member_name]" id="project_members_attributes_2_member_name">
以下略
```
パラメータ

```
Parameters:
{"utf8"=>"✓", "authenticity_token"=>"q58Q8Qsy9Q==",
"project"=>{"project_name"=>"テスト",
"members_attributes"=>
{"0"=>{"member_name"=>"hoge1"},
"1"=>{"member_name"=>"piyo2"},
"2"=>{"member_name"=>"hoge3"},
"3"=>{"member_name"=>"fuga4"},
"4"=>{"member_name"=>"piyopiyo5"},
"5"=>{"member_name"=>"member6"},
"6"=>{"member_name"=>"member7"},
"7"=>{"member_name"=>"member8"}}}, "commit"=>"登録"}
```

SQL

```sql
SQL (28.1ms)  INSERT INTO `projects` (`project_name`, `created_at`, `updated_at`) VALUES ('テスト', '2018-07-02 00:55:35', '2018-07-02 00:55:35')
SQL (1.0ms)  INSERT INTO `members` (`member_name`, `created_at`, `updated_at`, `project_id`) VALUES ('hoge1', '2018-07-02 00:55:35', '2018-07-02 00:55:35', 3)
SQL (0.0ms)  INSERT INTO `members` (`member_name`, `created_at`, `updated_at`, `project_id`) VALUES ('piyo2', '2018-07-02 00:55:35', '2018-07-02 00:55:35', 3)
SQL (1.0ms)  INSERT INTO `members` (`member_name`, `created_at`, `updated_at`, `project_id`) VALUES ('hoge3', '2018-07-02 00:55:35', '2018-07-02 00:55:35', 3)
SQL (1.0ms)  INSERT INTO `members` (`member_name`, `created_at`, `updated_at`, `project_id`) VALUES ('fuga4', '2018-07-02 00:55:35', '2018-07-02 00:55:35', 3)
SQL (1.0ms)  INSERT INTO `members` (`member_name`, `created_at`, `updated_at`, `project_id`) VALUES ('piyopiyo5', '2018-07-02 00:55:35', '2018-07-02 00:55:35', 3)
SQL (0.0ms)  INSERT INTO `members` (`member_name`, `created_at`, `updated_at`, `project_id`) VALUES ('member6', '2018-07-02 00:55:35', '2018-07-02 00:55:35', 3)
SQL (0.0ms)  INSERT INTO `members` (`member_name`, `created_at`, `updated_at`, `project_id`) VALUES ('member7', '2018-07-02 00:55:35', '2018-07-02 00:55:35', 3)
SQL (1.0ms)  INSERT INTO `members` (`member_name`, `created_at`, `updated_at`, `project_id`) VALUES ('member8',  '2018-07-02 00:55:35', '2018-07-02 00:55:35', 3)
```