2017-03-17 65 views
0

我和我的小应用程序的一个问题建立的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) 
+0

好的,只有一个疑问。你应该选择'@usercustom = Custom.where(user_id:@ user.id)'?这只是一个疑问。那么@usercustom有多少条目,它们应该有多少?您可以包含从@ @ usercustom获取的值和应该从'customs'获取的值。所以根据我的理解,听到您的问题只是查询'@usercustom = Custom.where(user_id:@user)',它检索一个等于'nil'的附加值。也许你在“海关”表中有这个价值? –

+0

Fabrizio我使用自定义表格中的一条记录提交联系人/新表单时,我使用日志消息编辑了帖子。 – CristiAllan

+0

我发布了解决方案,并更新了它,请阅读 –

回答

0

试评或删除@contact.contact_customs.buildcreate行动。这就是你有一个N + 1 contact_customs的原因,所有字段= nil。

当你做@contact = Contact.new(contact_params)创建@contact看起来应该像这样的:

@contact = Contact.new(:email => contact_params[:email], :name => contact_params[:name], ..etc..., :custom_attributes => contact_customs_attributes: [ :id, :value, :custom_id, custom_attributes: [] ]) 

有了,你有一个@contact对象实例化,并为数组:

@contact.contact_customs = [ first_contact_custom => [firstvalue, secondvalue], second_contact_custom => [firstvalue, secondvalue]] 

以下:

@contact.contact_customs.build 

就像在做

@custom = Custom.new() 
@contact.customs << @custom 

这将追加在连接表contact_customs@custom进入与各界= nil

@contact.contact_customs = [ first_contact_custom => [firstvalue, secondvalue], second_contact_custom => [firstvalue, secondvalue], second_contact_custom => [nil, nil] ] 

因此,尝试删除以下行

@contact.contact_customs.build 

在你需要该行是在new活动的地方,因为你需要实例化形式的那些字段。