title: Rails create join table object should not create but update in special case
body: <p>In a webshop I have a booking that needs to know if a booking already exists in the order. I had the whole thing working, but then the details...</p>
  
  <p>...now 'booking on a product' (or in normal English: Adding a product to your shopping-cart) adds a totally new booking on the order list in each case. It oughtn't when this product is already booked once, then it only should alter the quantity. </p>
  
  <p>So easy right? Just one simple if-statement and the whole thing works. </p>
  
  <p><strong>bookings_controller.rb</strong></p>
  
  <pre class="lang-ruby prettyprint-override"><code>  def create
      @order = current_order
      # If product has already been booked
      if @order.bookings.where(product_id: params[:product_id]).exists?
        # Then: Only alter the quantity in the booking.
        @booking = @order.bookings.where(product_id: params[:product_id])
        @booking.product_quantity = params[:product_quantity]
      else
        # Else: Make a new booking.
        @booking = @order.bookings.new(booking_params)
        @product = @booking.product
        @booking.product_name = @product.name
        @booking.product_price = @product.price
      end
      @order.sum_all_bookings 
      @order.save
    end
  
    # ...
  
    def booking_params
      params.require(:booking).permit(:product_quantity, :product_id)
    end
  </code></pre>
  
  <p>Doesn't seem to work. </p>
  
  <p>How do I make the check in the if-statement? 
  Or should I go about a whole different route to update the booking?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I've tried various combinations in this shape after gmcnaughton answer. I get either multiple entries still or no entries at all. This one gives me no entries at all.</p>
  
  <p><strong>bookings_controller.rb</strong></p>
  
  <pre class="lang-ruby prettyprint-override"><code>  def create
      @order = current_order
      @booking = @order.bookings.find_or_create_by(product_id: params[:product_id])
      product = @booking.product
      if @booking.new_record?
        @booking.product_name = product.name
        @booking.product_price = product.price
      else
      @booking.product_quantity = params[:product_quantity]
      @booking.save
      @order.sum_all_bookings 
      @order.save
    end
  </code></pre>
  
  <p>Should I also control the booking <code>id</code> maybe? But this makes no sense, because in the case of finding an existing booking it should be there already.</p>
  
  <p>Is it maybe that I am firing the bookings through a <code>remote: true form</code>?</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>Also not working:</p>
  
  <p><strong>bookings_controller.rb</strong></p>
  
  <pre class="lang-ruby prettyprint-override"><code>  def create
      @order = current_order
      @booking = @order.bookings.where(product_id: params[:product_id]).first_or_initialize
      if @booking.new_record?
        @booking.product_id = params[:product_id]
        product = @booking.product
        @booking.product_name = product.name
        @booking.product_price = product.price
      else
      @booking.product_quantity = params[:product_quantity]
      @booking.save
      @order.sum_all_bookings 
      @order.save
    end
  </code></pre>
  
  <p><strong>Edit 3</strong></p>
  
  <p>Maybe this has got something to do with it:</p>
  
  <p><strong>categories_controller.rb / Shop</strong></p>
  
  <pre class="lang-ruby prettyprint-override"><code>def index
      @categories = Category.all.order(name: :asc)
      # Voor het inzien van wat al geselecteerd is.
      @order = current_order
      # Voor het aanslaan van een nieuwe booking.
      @booking = current_order.bookings.new
    end
  </code></pre>
  
  <p>Which basically lays out the whole initial shop. The @booking is there to build the form of every product. </p>
  
  <p><strong>Following worked:</strong></p>
  
  <pre class="lang-ruby prettyprint-override"><code>  def create
      @booking = @order.bookings.find_by(product_id: params[:booking][:product_id])
      if @booking
        @booking.product_quantity = params[:booking][:product_quantity]
        @booking.save
      else
        @booking = @order.bookings.new(booking_params)
        @product = @booking.product
        @booking.product_name = @product.name
        @booking.product_price = @product.price
      end
      @order.save
    end
  </code></pre>
  
  <p>Apparently I needed to grab the params, by adding <code>[:booking]</code> like in <code>params[:booking][:product_id]</code>. Anybody knows why?</p>