In a purchase form, I add the product plus the requisite data, however, I need that purchase form to accept more than one product in it, this could be achieved using nested form + the coconut gem , however, the attributes belong to the same model, so the fastest logic that comes to me is to create 2 different models, to then be replicating the fields, however, I do not know if there is a way to replicate those fields without the need to create a separate model that will only serve me to nest them later, I share the essential code of the problem:
Purchase form
<div id="form-modal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
Producto
</div>
<%= form_with(model: @input, remote: true) do |form| %>
<div class="modal-body">
<div class="row">
<!-- Errors -->
<div class="col-sm-12">
<% if @input.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@input.errors.count, "error") %> prohibited this input from being saved:</h2>
<ul>
<% @input.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
<!-- Fields -->
<div class="col-sm-12">
<div class="form-group">
<% form.label :invoice %>
<%= form.text_field :invoice, placeholder: "Codigo de la factura", class: "form-control" %>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<% form.label :product_id %>
<%= form.collection_select :product_id, Product.all, :id, :code, { :include_blank => "Seleccionar producto" }, required: true, class: "form-control" %>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<% form.label :quantity %>
<%= form.text_field :quantity, placeholder: "Cantidad", class: "form-control" %>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<% form.label :precio %>
<%= form.text_field :price, placeholder: "Precio", class: "form-control" %>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<% form.label :utilidad %>
<%= form.text_field :utility, placeholder: "Porcentaje de utilidad", class: "form-control" %>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<% form.label :customer %>
<%= form.text_field :provider, placeholder: "Proveedor", class: "form-control" %>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<%= form.submit "Enviar", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
Buy Model (Input)
class Input < ApplicationRecord
belongs_to :product
validates :quantity, numericality: true
validates :invoice, uniqueness: true
end
Model Products
class Product < ApplicationRecord
has_many :inputs
end
Migrate Purchase
class CreateInputs < ActiveRecord::Migration[5.2]
def change
create_table :inputs do |t|
t.string :invoice
t.integer :product_id
t.float :quantity
t.float :price
t.float :utility
t.string :provider
t.timestamps
end
end
end
Product migration
class CreateProducts < ActiveRecord::Migration[5.2]
def change
create_table :products do |t|
t.string :code
t.string :name
t.timestamps
end
end
end
Basically I need to replicate all the fields of purchase (Input), except for the invoice number (Invoice), however as I mention again, all these attributes belong to the same model, so I do not know in what way I should handle This is not to have 2 different models that are nested, I appreciate your suggestions