Matias
Matias

Reputation: 13

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

i'm using rails 4.2 and i have a form that are attempting to make an insert into the Response model for each checkbox of a collection_check_boxes. When i do that i don't have any errors and one of the attributes, adjective_id, it's not being saved.

Here you can see how the param looks like when i choose 4 of 58 checkboxes:

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

Here the insert:

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"]]

I need to get the adjective_id into the insert statement, something like this:

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

Models

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

Controller

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

Routes

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

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

I hope it's a clear question. I'm noob with ruby on rails so i would be gratefull for any answer to clarify my questions.

Thanks!

Upvotes: 1

Views: 998

Answers (1)

Matias
Matias

Reputation: 13

I don't know if this is the best answer to do this, but now is the only one i have.

    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

Then i have the result that i was looking for: https://i.sstatic.net/wBPlw.jpg

If someone knows a better way to get this, please let me know. Thanks

Upvotes: 0

Related Questions