0

I have following arragement

The Pizza model, to create a list of pizzas that can be ordered by customers, also gets associated with order, so to indicate which pizza has been ordered.

class Pizza < ActiveRecord::Base  
  has_many :pizza_orders
  has_many :orders, :through => :pizza_orders
  has_and_belongs_to_many :toppings
end

Option model, to create a list of options that can be associated with certain pizzas, also gets associated with the join table for each pizza order, to specify which pizza has the topping ordered.

class Topping < ActiveRecord::Base
  has_and_belongs_to_many :pizzas
  has_and_belongs_to_many :pizza_orders
end

The join table for pizza and topping, this is needed because without it, you can not specify which toppings can or can not be ordered with a pizza. After all, having peperoni topping listed to a vegetarian pizza might offend someone.

class PizzasToppings < ActiveRecord::Base
  belongs_to :pizza
  belongs_to :topping
end

The order model, this just holds all the join tables together.

class Order < ActiveRecord::Base
  has_many :pizza_orders
  has_many :pizzas, :through => :pizza_orders
end

The join table between pizza and order, this many to many is a has many through, and not a has and belongs to many since in Rails you cannot directly manipulate HBATM join tables (as far as I have tried), and you need to be able to because of the options relation ship.

class PizzaOrder < ActiveRecord::Base
  belongs_to :pizza
  belongs_to :order

  has_and_belongs_to_many :toppings
end

The join table to indicate which toppings have been chosen for a particular pizza in a order.

class PizzaOrdersToppings < ActiveRecord::Base
  belongs_to :pizza_orders
  belongs_to :topping
end

Then I have an admin page to create and associate the Pizzas and toppings.

But I don't know how to create the orders form. The user should be able to add a pizza and select one or many toppings which are already created

2 Answers 2

2

Its a bit old screencast Nested model form part 1 made by ryan bates but i hope it helps you , also there is a revised version of this screencast.

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

1 Comment

this wouldn't work, ryan is creating new questions and answer. I just want to ADD questions and answers to an order not create.
1

Sorry I was late to respond.

Okay, haven't tested this, but hope it gets you the idea. In order for it to work, this sample relies on,

  1. simple_form
  2. the ryan bates dynamic nested form

I have to apologize, this answer is lacking a large part of it, you need to re-render the link_to_add_fields part every time you change the <%= f.association :pizzas %> value so that the hidden field that contains the toppings match the pizza that has been selected.

Sorry I couldn't get you completly through, but I hope this points you in the right direction.

_form.html.erb

<%= simple_form @order do |f| %>

  <%= f.simple_fields_for :pizza_orders do |pizza_orders_fields| %>
    <%= f.association :pizzas %>
    <%= render partial: "pizza_order_fields", locals: {f: pizza_orders_fields}%>
    <%= link_to_add_fields "Add", f, :pizza_orders %>
  <% end %>
<% end %>

pizza_order_fields.html.erb would look like

<fieldset>
  <%= f.hidden_field :_destroy %>
  <%= link_to "Delete", '#', class: "remove_fields btn btn-danger" %>
  <%= f.association toppings_pizza_orders, collection: f.object.pizza.toppings %>
</fieldset>

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.