【问题标题】:Can't insert Array of id's into join table with has_many :through association无法使用 has_many 将 id 数组插入连接表:通过关联
【发布时间】:2015-02-11 03:20:11
【问题描述】:

我正在使用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 ($1, $2, $3, $4) 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")
  • 我怎样才能得到这个,才能正确插入?
  • 有什么方法可以在 Responses 表中为 params 数组中的每个“adjective_id”创建具有相同“relation”、“survey_id”、“created_at”和“updated_at”的 1 行?

模型

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 %>

我希望这是一个明确的问题。我是 ruby​​ on rails 的菜鸟,所以如果有任何答案来澄清我的问题,我将不胜感激。

谢谢!

【问题讨论】:

  • 你看过this的问题及其答案吗?
  • 好的,我刚刚看了那篇文章,谢谢你的回答。所以在我的情况下,参数 :adjective_id 是允许的。现在,分配值的唯一方法是拆分我的数组,就像在那个答案中一样?以及为什么 rails 无法识别 adjective_id 是对象响应的属性?

标签: ruby-on-rails ruby-on-rails-4 strong-parameters jointable


【解决方案1】:

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

    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

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

【讨论】:

  • @response 有多个adjective_id 时会不会出错?您可能需要考虑使用:@response = adjective_ids.map { |adj_id| @survey.responses.create(response_params adj_id) }
  • 是的,你是对的,我会更新我的答案。感谢指正
猜你喜欢
  • 2014-02-05
  • 1970-01-01
  • 2016-03-01
  • 2017-12-20
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多