mercredi 14 août 2019

Multiple Select many to many Ruby on Rails not creating associative records

Well, I have a many to many association between Contacts and Lawsuits on a table named Actives. In this case, I'm trying to add many contacts to a lawsuit. The problem is: the actives not saving in lawsuits if select is multiple, only if multiple: false.

Here's the models:

    #contact.rb
    class Contact < ActiveRecord::Base
    ...
    has_many :actives
    has_many :lawsuits, through: :actives

    #lawsuit.rb
    class Lawsuit < ActiveRecord::Base
    ...
    has_many :actives
    has_many :contacts, through: :actives

    accepts_nested_attributes_for :actives, allow_destroy: true

    #active.rb
    class Active < ActiveRecord::Base
    ...
    belongs_to :lawsuit
    belongs_to :contact

And Here's the migration table:

    class CreateActives < ActiveRecord::Migration
     def change
      create_table :actives do |t|
       t.belongs_to :contact, index: true
       t.belongs_to :lawsuit, index: true
       t.datetime :appointment_date
       t.timestamps
      end
     end
    end

The controller:

    #controller/lawsuit.rb
    #create method

    def create
      @lawsuit = Lawsuit.new(lawsuit_params)

      respond_to do |format|
        if @lawsuit.save
          format.html { redirect_to @lawsuit, notice: 'Lawsuit was successfully created.' }
          format.json { render :show, status: :created, location: @lawsuit }
        else
          format.html { render :new }
          format.json { render json: @lawsuit.errors, status: :unprocessable_entity }
        end
      end
    end

    #require params
    params.require(:contact).permit(:name, :lastname, :cpf, :rg, :birthdate, :profession_id, :marital_status_id, :address,
                                    :zipcode, :city, :state, :district, :number, :actives)

The view and output params:

    #/views/lawsuits/_form.html.erb
    <%= form_for @lawsuit, html: {class: "ui form"} do |f| %>
    ...
    <%= f.fields_for :actives do |w| %>
      <%= w.select(:contact_id, Contact.all.pluck(:name,:id), {}, {class:"selectize-generic", multiple: true})  %>
    <% end %>
    <%= f.submit %>

    #Params:
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"9dpRjs0e2iQXgYzNqObSyzuvEnVdQYVHos922hbu0UptxiVeZfJgxgbfgFGKOUR16119VFLOfheNGogAOwez/w==", 
    "lawsuit"=>{"autos"=>"", "forum_id"=>"1", "lawyer_id"=>"4", "conciliation_date"=>"", "instruction_date"=>"", "fees"=>"",
    "actives_attributes"=>{"0"=>{"contact_id"=>["", "2", "7", "9"]}}}, "commit"=>"Create Lawsuit"}

In last saved Lawsuit:

    rails c
    x = Lawsuit.last
    => #<Lawsuit id: 17, forum_id: 1, lawyer_id: 4, fees: nil, autos: "", conciliation_date: "", instruction_date: "", created_at: "2019-08-14 15:43:18", updated_at: "2019-08-14 15:43:18">
    x.actives
    => #<ActiveRecord::Associations::CollectionProxy [#<Active id: 9, contact_id: nil, lawsuit_id: 17, appointment_date: nil, created_at: "2019-08-14 15:43:18", updated_at: "2019-08-14 15:43:18">]>

I've been wasting a lot of time on this, I've already try everything, recreate models, tried simple_form gem, change in params, etc. I'm lost. Anyone can help?

GitHub for the project: https://github.com/killerowns/lawsuit-manager-rails

Aucun commentaire:

Enregistrer un commentaire