I know how to build the 2nd object in a controller but how do you build a third or a fourth?
In my case i need to build 3.
Location - has_many :product_dates, :products
ProductDate - has_many :products & belongs_to :location
Product - belongs_to :location, :product_date
I build the Location and Product Date easily:
def new
@location = Location.new
@location.product_dates.build
end
Now i need to build the products on the form. Can anyone show me how to do this?
EDIT: Complete Answer:
def new
@location = Location.new
product_date = @location.product_dates.build
product_date.products.build
end
<%= form_for @location do |f| %>
<%= f.text_field :business %>
<%= f.text_field :address %>
<%= f.fields_for :product_dates do |date| %>
<%= date.date_select :date %>
<%= date.fields_for :products do |product| %>
<%= product.text_field :name %>
<%= product.text_field :price %>
<%= product.text_field :tag_list %>
<% end %>
<% end %>
<%= f.submit "Create" %>
<% end %>
You’ll learn everything in video here.
EDIT:
change the nested part with:
Because
productsare nested insideproduct_dates