Proper way to initialize template variables within a loop?

This is from an inherited codebase (I didn’t write this):

<% purchase_order.line_items.each do |line_item| %>
  <%
    location_product = line_item.location_product
    product = location_product.product
    par_min = location_product.par_min || 0
    par_max = location_product.par_max || 0
    on_hand = location_product.on_hand || 0
    on_request = location_product.on_request || 0
    on_order = product.on_order
    default_vendor_sku = line_item.product.prices.where(vendor_id: purchase_order.vendor.id).first&.vendor_sku rescue nil
  %>

This doesn’t seem very MVC, since variables are generally initialized in the controller, from how I understand MVC.

It looks like the person who wrote this wanted to create shorter variables based on the records being iterated through.

Is there a more Rails way to do this?

Yikes. I was going to say a helper method, but then I scrolled right and saw the database query…

I would do this as instance methods and some delegate calls in the LineItem model. Then it’s available when you restructure this to something like <%= render purchase_order.line_items %>.

Walter