0

我試圖設置Rails 5 has_many通過:三種模型上的關聯類型。目標是讓用戶可以填寫該編輯或創建所有三種模型(和關聯)。Rails 5 has_many通過:關聯爲中介模型創建2條記錄

項目是「中介」模式。

這裏是我的模型:

class Client < ApplicationRecord 
    has_many :projects 
    has_many :programmers, through: :projects 

    accepts_nested_attributes_for :projects, reject_if: :all_blank, allow_destroy: true 
    accepts_nested_attributes_for :programmers, reject_if: :all_blank, allow_destroy: true 
end 

class Project < ApplicationRecord 
    belongs_to :client, optional: true 
    belongs_to :programmer, optional: true 
end 

class Programmer < ApplicationRecord 
    has_many :projects 
    has_many :clients, through: :projects 
end 

Postgres的模式:

create_table "clients", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.string "name" 
end 

create_table "programmers", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.string "name" 
end 

create_table "projects", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.string "name" 
    t.integer "client_id" 
    t.integer "programmer_id" 
    t.index ["client_id"], name: "index_projects_on_client_id", using: :btree 
    t.index ["programmer_id"], name: "index_projects_on_programmer_id", using: :btree 
end 

add_foreign_key "projects", "clients" 
add_foreign_key "projects", "programmers" 

客戶端控制器(只是新/創建& PARAM部分)

def new 
    @client = Client.new 
    @client.projects.build 
    @client.programmers.build 
end 

def create 
    @client = Client.new(client_params) 

    respond_to do |format| 
    if @client.save 
     format.html { redirect_to @client, notice: 'Client was successfully created.' } 
     format.json { render :show, status: :created, location: @client } 
    else 
     format.html { render :new } 
     format.json { render json: @client.errors, status: :unprocessable_entity } 
    end 
    end 
end 

private 

    def client_params 
    params.require(:client).permit(:id, :name, projects_attributes: [:id, :name], programmers_attributes: [:id, :name]) 
end 

最後,形式:

<%= form_for(client) do |f| %> 
<% if client.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(client.errors.count, "error") %> prohibited this client from being saved:</h2> 

     <ul> 
     <% client.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <label>Client name</label> 
    <%= f.text_field :name %> 
    <br /> 

    <%= f.fields_for :projects do |projectfields| %> 
    <label>Project name</label> 
    <%= projectfields.text_field :name %> 
    <% end %> 
    <br /> 

    <%= f.fields_for :programmers do |programmerfields| %> 
    <label>Programmer name</label> 
    <%= programmerfields.text_field :name %> 
    <% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

控制檯日誌:

Started POST "/clients" for ::1 at 2016-11-09 09:14:31 -0800 
Processing by ClientsController#create as HTML 
    Parameters: {"utf8"=>"✓",  "authenticity_token"=>"6QouF/zC+ivHAlbEvnhuBob80/AGt2QFmn0xa569+wJ8iWRmDOnz229OJq6PxaYRnzjNoDf8j71Jf2FpY3FMZw==", "client"=>{"name"=>"aaa", "projects_attributes"=>{"0"=>{"name"=>"bbb"}}, "programmers_attributes"=>{"0"=>{"name"=>"ccc"}}}, "commit"=>"Create Client"} 
(0.2ms) BEGIN 
SQL (0.5ms) INSERT INTO "clients" ("created_at", "updated_at", "name") VALUES ($1, $2, $3) RETURNING "id" [["created_at", 2016-11-09 17:14:31 UTC], ["updated_at", 2016-11-09 17:14:31 UTC], ["name", "aaa"]] 
SQL (0.6ms) INSERT INTO "projects" ("created_at", "updated_at", "name", "client_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", 2016-11-09 17:14:31 UTC], ["updated_at", 2016-11-09 17:14:31 UTC], ["name", "bbb"], ["client_id", 1]] 
SQL (0.4ms) INSERT INTO "programmers" ("created_at", "updated_at", "name") VALUES ($1, $2, $3) RETURNING "id" [["created_at", 2016-11-09 17:14:31 UTC], ["updated_at", 2016-11-09 17:14:31 UTC], ["name", "ccc"]] 
SQL (0.4ms) INSERT INTO "projects" ("created_at", "updated_at", "client_id", "programmer_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", 2016-11-09 17:14:31 UTC], ["updated_at", 2016-11-09 17:14:31 UTC], ["client_id", 1], ["programmer_id", 1]] 
(5.5ms) COMMIT 
Redirected to http://localhost:3000/clients/1 
Completed 302 Found in 19ms (ActiveRecord: 7.5ms) 

所以基本上合作,爲所有三種模式創建條目,除非有看起來像這樣的項目創建2個DB條目:

enter image description here

我一直在盯着這太久,圖我一定是失去了明顯的東西。有任何想法嗎?謝謝您的幫助。

回答

0

給這一個鏡頭,看看它的工作原理:

<%= form_for @client do |f| %> 

<% if client.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(client.errors.count, "error") %> prohibited this client from being saved:</h2> 

     <ul> 
     <% client.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <label>Client name</label> 
    <%= f.text_field :name %> 
    <br /> 

    <%= f.fields_for @client.projects do |projectfields| %> 
    <label>Project name</label> 
    <%= projectfields.text_field :name %> 
    <% end %> 
    <br /> 

    <%= f.fields_for @client.programmers do |programmerfields| %> 
    <label>Programmer name</label> 
    <%= programmerfields.text_field :name %> 
    <% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
+0

我覺得這是更接近 - 一堆大驚小怪的樣子的關鍵可能是窩fields_for項目內field_for程序員阻斷後? – PDD

相關問題