无法将 ID 数组插入到 table 与 has_many 的连接中:通过关联

Can't insert Array of id's into join table with has_many :through association

我正在使用 rails 4.2,我有一个表单试图为 collection_check_boxes 的每个复选框插入响应模型。当我这样做时,我没有任何错误和属性之一,adjective_id,它没有被保存。

在这里你可以看到当我选择 58 个复选框中的 4 个时参数的样子:

"response"=>{"relation"=>"family", "adjective_id"=>["2", "14", "16", "28", ""]}, "commit"=>"Answer", "survey_sruid"=>"d5e7b675370614d1331f"}

此处插入:

INSERT INTO "responses" ("relation", "survey_id", "created_at", "updated_at") VALUES (, , , ) RETURNING "id"  [["relation", "3"], ["survey_id", 1], ["created_at", "2015-02-11 02:39:41.409276"], ["updated_at", "2015-02-11 02:39:41.409276"]]

我需要将 adjective_id 放入插入语句中,如下所示:

INSERT INTO "responses" ("relation", "survey_id", "adjective_id", "created_at", "updated_at")

型号

class Survey < ActiveRecord::Base
 has_many :responses
 has_many :adjectives, through: :responses
 belongs_to :nruser
end

class Response < ActiveRecord::Base
 belongs_to :survey
 belongs_to :adjective
end

class Adjective < ActiveRecord::Base
 has_many :responses
 has_many :surveys, through: :responses
end

Schema.rb: here

控制器

class ResponsesController < ApplicationController

def new
    @survey = Survey.find_by(sruid: params[:survey_sruid])
    @response = @survey.responses.build
end

def create
    @survey = Survey.find_by(sruid: params[:survey_sruid])
    @response = @survey.responses.create(response_params)
    redirect_to root_path
end

private

    def response_params
        params.require(:response).permit(:relation, { :adjective_id => [] })
    end
end

路线

Rails.application.routes.draw do

 root                'static_pages#home'
 resources :surveys, only: [:new, :create, :show, :destroy]
 resources :surveys, param: :sruid, only: [:create] do
    resources :responses, only: [:new, :create]  
 end
 resources :adjectives, only: [:index]
end

表格

<%= form_for @response, url: survey_responses_path do |r| %>
    <aside class="col-md-3">
        <%= r.label :relation, @survey.nruser.name.titleize + " es un:" %>
        <p><%= r.select :relation, options_for_select([['familiar', 3], ['amigo', 2], ['colega', 1], ['conocido',0]]) %></p>
        </br>
        <%= r.submit "Responder YA!", class: "btn btn-success" %>
    </aside>

    <aside class="col-md-9">
        <div class="btn-group" data-toggle="buttons">
            <%= r.collection_check_boxes :adjective_id, Adjective.all, :id, :adjective do |b| 
                    b.label(class: 'btn btn-primary') {b.check_box + b.text}
            end %>
        </div>
    </aside>
<% end %>

我希望这是一个明确的问题。我是 rails 上 ruby 的菜鸟,所以我将不胜感激任何能澄清我的问题的答案。

谢谢!

我不知道这是否是最好的答案,但现在是我唯一的答案。

    def create

     @survey = Survey.find_by(sruid: params[:survey_sruid])

     #Because collection_check_boxes adds a blank value at the end of the array.
     adjectives_ids = params[:response][:adjective_id].reject! { |c| c.empty? }

     adjectives_ids.each do |adj_id|
         @response = adjectives_ids.map { |adj_id| @survey.responses.create(response_params adj_id)
     end

     redirect_to root_path
    end

 private

    def response_params(adj_id)
        params.require(:response).permit(:relation).merge(adjective_id: adj_id)
    end

然后我得到了我想要的结果: http://imgur.com/vXnnwdb

如果有人知道更好的方法,请告诉我。谢谢