title: Rails: Structuring rental orders & order form in ecommerce-like setting
body: <p>I was wondering if someone could help me out with an application that has some ecommerce characteristics.  </p>
  
  <p><strong>Context:</strong> Via the application a bike shop chain ('chains') can rent out </p>
  
  <ul>
  <li>bikes ('<em>bikes</em>'), </li>
  <li>by picking out a bike type such as mountainbike, city bike etc. ('<em>bike_types</em>) and </li>
  <li>bike options, such as helmets etc. ('<em>bike_options</em>') </li>
  <li>which are dependent on the individual bike store ('<em>bike_stores</em>')</li>
  <li>this rental of the bikes &amp; options will all be captured in an order ('<em>orders</em>')</li>
  <li>the relationship between orders and bikes is many-to-many, therefore I created a  table to bridge this ('<em>order_bikes</em>')</li>
  </ul>
  
  <p><strong>Final notes:</strong> </p>
  
  <ul>
  <li>Before the rental process, the chain owner first created his/her <em>(i) bike_stores, (ii) bike_types, (iii) bikes and (iv) bike_options</em>, this part of the application is working. Therefore, he/she only needs to select bike_types/bikes/options out of the existing inventory previously created.</li>
  <li>I limit the scope of the question by leaving out the bike_options, this was mainly to provide some context in order to understand the db schema build up.</li>
  </ul>
  
  <p><strong>Error message:</strong> Unpermitted parameter: :bike_id</p>
  
  <p><strong>Code:</strong></p>
  
  <p><em>models</em></p>
  
  <pre><code>class Order &lt; ApplicationRecord
    belongs_to :bike_store
    has_many :bike_types, through: :bike_store
    has_many :order_bikes, inverse_of: :order, dependent: :destroy
    accepts_nested_attributes_for :order_bikes, allow_destroy: true
  end
  
  
  class OrderBike &lt; ApplicationRecord
    belongs_to :bike
    belongs_to :order
    accepts_nested_attributes_for :bike
  end
  
  
  class Bike &lt; ApplicationRecord
    belongs_to :bike_type
    validates :name, presence: true
    has_many :order_bikes
    has_many :orders, through: :order_bikes
  end
  
  
  class BikeType &lt; ApplicationRecord
    belongs_to :bike_store
    has_many :bikes, dependent: :destroy
    accepts_nested_attributes_for :bikes, allow_destroy: true
    has_many :bike_options, dependent: :destroy
    accepts_nested_attributes_for :bike_options, allow_destroy: true
    validates :name, :bike_count, presence: true
  end
  
  class BikeStore &lt; ApplicationRecord
    has_many :bike_types, dependent: :destroy
    has_many :orders, dependent: :destroy
  end
  </code></pre>
  
  <p>Order controller</p>
  
  <pre><code>class OrdersController &lt; ApplicationController
  
    def new
      @bike_store = BikeStore.find(params[:bike_store_id])
      @order = Order.new
      @order.order_bikes.build
      @bike_type_list = @bike_store.bike_types
    end
  
    def create
      @order = Order.new(order_params)
      @bike_store = BikeStore.find(params[:bike_store_id])
      @order.bike_store = @bike_store
      @order.save
      redirect_to root_path
    end
  
  private
    def order_params
      params.require(:order).permit(:arrival, :departure,
        order_bikes_attributes: [:id, :bike_quantity, :_destroy,
          bikes_attributes: [:id, :name,
            bike_types_attributes: [:id, :name]]])
    end
  end
  </code></pre>
  
  <p>view</p>
  
  <pre><code>&lt;%= simple_form_for [@bike_store, @order] do |f|%&gt;
  
  &lt;%= f.simple_fields_for :order_bikes do |order_bike| %&gt;
    &lt;%= order_bike.input :bike_quantity %&gt;
    &lt;%= order_bike.association :bike %&gt;
  &lt;% end %&gt;
  
   &lt;%= f.input :arrival %&gt;
   &lt;%= f.input :departure %&gt;
   &lt;%= f.submit %&gt;
  &lt;% end %&gt;
  </code></pre>