我和我的小應用程序的一個問題建立的Rails 5Rails的5個註冊嵌套屬性HAS_MANY通過與正協會+ 1
項目只有4個表:用戶,自定義,聯繫人,ContactCustom
這個想法是一個用戶註冊他的海關字段,當他要添加新的聯繫人時,表單應該顯示用戶登錄的海關。
我的問題是當我嘗試註冊一個新的聯繫人的海關用戶登錄後,我有一個n + 1在ContactCustom表中插入一個零寄存器,並且不捕獲我用hidd傳遞的custom_id en_field。
我的模式是這樣的:
class Custom < ApplicationRecord
belongs_to :user
belongs_to :kind
has_many :contact_customs
has_many :contacts, through: :contact_customs
end
class ContactCustom < ApplicationRecord
belongs_to :contact, optional: true
belongs_to :custom, optional: true
accepts_nested_attributes_for :custom
end
class Contact < ApplicationRecord
belongs_to :user
has_many :contact_customs
has_many :customs, through: :contact_customs
accepts_nested_attributes_for :contact_customs
end
這裏我contact_controller:
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
before_action :set_user_and_custom, only: [ :new, :create, :edit ]
def index
@contacts = Contact.all
end
def show
end
def new
@contact = Contact.new
@contact.contact_customs.build
end
def edit
end
def create
@contact = Contact.new(contact_params)
@contact.contact_customs.build
#binding pry
respond_to do |format|
if @contact.save
format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
format.json { render :show, status: :created, location: @contact }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
private
def set_contact
@contact = Contact.find(params[:id])
end
def set_user_and_custom
@user = current_user
@usercustom = Custom.where(user_id: @user)
end
def contact_params
params.require(:contact).permit(:email, :name, :user_id,
contact_customs_attributes: [ :id, :value, :custom_id, custom_attributes: [] ])
end
end
,這裏是我的形式......我想,我做了一些錯誤與每個循環:
<% @usercustom.each do |c| %>
<%= f.fields_for :contact_customs do |cc| %>
<div class="field">
<%= cc.label :value, c.name %>
<%= cc.text_field :value %>
</div>
<%= cc.fields_for :custom do |custom| %>
<%= custom.text_field :id, value: c.id %>
<% end %>
<% end %>
<% end %>
我不知道如何顯示有多少自定義字段沒有這個循環和我的查詢〜> @u sercustom = Custom.where(user_id:@user)正在註冊一個更多的零記錄(n + 1)。
這裏是日誌消息時,我只有一個自定義記錄提交聯繫表單自定義表:
Started POST "/contacts" for 127.0.0.1 at 2017-03-17 09:14:02 -0300
Processing by ContactsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"nIlwxH4Ua8DjAKkMpGh8B7nxwYf6gy1Fhkdh1PaMtSANx5sB6YaOKbBUekQ4M3KP56WuHgsX31iHq2lj4+fEwA==", "contact"=>{"email"=>"[email protected]", "name"=>"name test", "user_id"=>"1", "contact_customs_attributes"=>{"0"=>{"value"=>"custom test"}}}, "commit"=>"Create Contact"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
SQL (108.3ms) INSERT INTO "contacts" ("email", "name", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["email", "[email protected]"], ["name", "name test"], ["user_id", 1], ["created_at", 2017-03-17 12:14:02 UTC], ["updated_at", 2017-03-17 12:14:02 UTC]]
SQL (7.7ms) INSERT INTO "contact_customs" ("value", "contact_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["value", "custom test"], ["contact_id", 3], ["created_at", 2017-03-17 12:14:02 UTC], ["updated_at", 2017-03-17 12:14:02 UTC]]
SQL (0.6ms) INSERT INTO "contact_customs" ("contact_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["contact_id", 3], ["created_at", 2017-03-17 12:14:02 UTC], ["updated_at", 2017-03-17 12:14:02 UTC]]
(36.5ms) COMMIT
Redirected to http://localhost:3000/contacts/3
Completed 302 Found in 176ms (ActiveRecord: 154.5ms)
好的,只有一個疑問。你應該選擇'@usercustom = Custom.where(user_id:@ user.id)'?這只是一個疑問。那麼@usercustom有多少條目,它們應該有多少?您可以包含從@ @ usercustom獲取的值和應該從'customs'獲取的值。所以根據我的理解,聽到您的問題只是查詢'@usercustom = Custom.where(user_id:@user)',它檢索一個等於'nil'的附加值。也許你在「海關」表中有這個價值? –
Fabrizio我使用自定義表格中的一條記錄提交聯繫人/新表單時,我使用日誌消息編輯了帖子。 – CristiAllan
我發佈瞭解決方案,並更新了它,請閱讀 –