Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Patrick Montalto
Patrick Montalto
7,868 Points

Header and Line Items Relationship in RoR

I've worked through some rails videos and nearly completed Michael Hartl's railstutorial and am about to make my own application.

One of the objects in my application is a Formulation, which has header information and multiple line items. When someone goes to create a new formulation, they would be presented with a page where they can type in the header information and then fill in multiple lines and save. Does anyone know of a good example with this type of structure? It's very similar to an Order, with an order having header information (shipping, client, etc) and line items (item, quantity, size, etc).

It is my understanding that I should create a model for each, with a FormulationHeader model having many FormulationLines which belong_to FormulationHeader. But then do each of them need a controller or do I just need one FormulationsController? I'm just not really sure where to go from here or if my understanding of how these models should be created and associated is proper.

Any guidance in the right direction would be much appreciated. Thanks.

1 Answer

If i understand correctly it sounds like you might just need two models to represent that. A Formulation which will contain attributes for the header information and FormulationLineItem which would contain all the attributes for each line item. The only case i could make for having FormulationHeader be its own model is if the information is re-used in many formulations and you want to reference all formulations that use that header info. Does that make sense?

To answer your second question about controllers it's really up to you. For cases like this where the information is usually entered at the same time it's common to just have a single controller, in this case FormulationsController and allow the Formulation model to accept nested attributes. In a case where the has_many model is added after the fact (e.g. comments on a blog post) it may make sense to have a separate controller to for CRUD actions on those objects.

  class Formulation < ActiveRecord::Base
    has_many :formulation_line_items
    accepts_nested_attributes_for :formulation_line_items

  class FormulationLineItem < ActiveRecord::Base
    belongs_to :formulation
Patrick Montalto
Patrick Montalto
7,868 Points

Thanks for the reply. I am going to start with this and see how it goes. Do you have any recommendations or recommended gems to provide the ability to create new blank row lines for FormulationLineItems on a new.html.erb form page? I'm picturing some sort of [+] to create a new blank row on the form so that the user could fill out multiple FormulationLineItems on the form. Thanks again.

I believe nested_form still works with Rails 4. It'll give you a lot of what you're looking for.