0

These are my params:

{"utf8"=>"✓", "authenticity_token"=>"0RYiIDDgmOk0gCDRkAgHvv+UIgp/BuU33CLThJXqOTE=", 
    "order"=>
            {"operation_in_orders_attributes"=>
                 {"0"=>{"service_operation_id"=>"5"}, 
                  "1"=>{"service_operation_id"=>""}, 
                  "2"=>{"service_operation_id"=>"4"}, 
                  "3"=>{"service_operation_id"=>""},                 
                  "4"=>{"service_operation_id"=>""}}, 
            "kontakt"=>"comment", "Car_id"=>"50"}, 
                      "commit"=>"Dodaj", 
                      "car_id"=>"dw815gn"}

Order has many operation_in_orders
Order has many service_operations through OperationInOrder
OperationInOrder belongs to Order
OperationInOrder belongs to ServiceOperation
ServiceOperation has many operation_in_orders
ServiceOperation has many orders through OperationInOrder

My form:

<%= form_for @order, url: new_car_order_path(@car, @order),  html: {class: "add_order"} do |r| %>
  <%= r.label "Service", class: :add_order_label    %> 
  <% 5.times do %>
  <%= r.fields_for :operation_in_orders do |v| %>

  <%= v.collection_select(:service_operation_id, ServiceOperation.all, :id, :nazwa,include_blank: true) %>
 <!-- <%= v.text_field :order_id, value: @order.id, :style => "display:none" %> -->
  <% end %>
  <% end %>
  <%= r.label "Kontakt", class: :add_order_label %>
  <%= r.text_field :kontakt %>
  <%= r.text_field :Car_id, value: @car.id, :style => "display:none" %>
  <%= r.label " "  %> 
  <%= r.submit "Add", class: "sub" %>
  <%= link_to "Send",ordered_path(car_id: @car.id) ,  class: 'sub'%> 

<% end %>

I have a form where I can choose five ServiceOperations at most to an order and save.
When I save, 5 new OperationInService objects/rows are made.

Is there a possibility to not create those join tables if corresponding field on form is blank?

For example:
I fill only 2 from 5 fields. I save only these two, not 5. Now I save nil values...

I have tried to validate in OperationInService model, but there was an error (rails do not recognize format in controller).

Any ideas?

2
  • 1
    Have you tried changing your params before creating the object: params[:order][:operation_in_orders_attributes].reject! { |op| op[:service_operations_id].blank? } Commented Apr 29, 2014 at 19:51
  • 1
    I have this error there: no implicit conversion of Symbol into Integer Commented Apr 29, 2014 at 20:12

1 Answer 1

0

Update the accepts_nested_form_for method call in Order model as below:

class Order < ActiveRecord::Base
  has_many :operation_in_orders
  accepts_nested_attributes_for :operation_in_orders, reject_if: proc { |attributes| attributes['service_operation_id'].blank? }
  ## ..
end

This way record for operation_in_orders would not be created if service_operation_id is blank.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.