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

Ruby

whats going on in this line, where is line_items coming from

<% @invoice.line_items.each do |li|%> Im curious on whats going on here @invoice is the instance variable dot opertaor does what to line_items ? And how is it reading line_items

located in invoice/show.html.erb

Grace Kelly
Grace Kelly
33,990 Points

Hi Ivan could you please post the code you are referring to??

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @invoice.name %></br>
  Total: <%= @invoice.total %>
</p>

<div>

  <h3>Line items</h3>
  <table>
    <th>Product</th>
    <th>Quantity</th>
   <th> Total Price</th>
    <% @invoice.line_items.each do |li|%>
    <tr>
      <td><%=li.product.name%></td>
      <td><%=li.quantity%></td>
      <td><%=li.total_price%></td>
    </tr>
    <% end %>
  </table>
</div>

<%= link_to 'Edit', edit_invoice_path(@invoice) %> |
<%= link_to 'Back', invoices_path %>

1 Answer

Tim Knight
Tim Knight
28,888 Points

Ivan,

You're probably looking at a relationship with another class called LineItems. Look to see if there is another model in the project that you're working with called LineItems.rb. You'll probably see that there is a line in that file that says belongs_to :invoice and within your invoice.rb model you should see has_many :line_items. So basically @invoice.line_items is an array of all of the line_items associated with a particular invoice.

Again, this is just my assumption based on the code you posted.