2017-10-12 25 views
0

我有一個Order模型,一個LineItem模型和一個產品模型。以下是這些協會。我試圖找出如何訪問產品模型中的name屬性。我的第一個想法是迭代LineItem表來收集line_items,其中order_id == order用於show view中。然後,遍歷新的line_items並從line_item的product_id屬性獲取每個產品名稱。這必須在一個方法中完成,可能是在Order模型中。有沒有更好的方法可以使用Rails關聯來完成?我目前的實施效果不確定的方法對產品#Order:0x007f5d007305f0迭代多個連接的表以顯示視圖中的項目

order.rb

class Order < ApplicationRecord 
    has_many :line_items 
    belongs_to :user 

    accepts_nested_attributes_for :line_items 

    after_validation :set_amount 

    private 
    def set_amount 
    self.amount = line_items.map(&:product).map(&:cost).inject(:+) 
    end 
end 

product.rb

class Product < ApplicationRecord 
    has_many :line_items 
end 

line_item.rb

class LineItem < ApplicationRecord 
    belongs_to :order 
    belongs_to :product 
end 

訂單/節目.html.erb

<h3>Customer - <%= @order.user.first_name %> <%= @order.user.last_name %></h3> 
<h4>Order Date - <%= @order.created_at.to_s(:date_us) %></h4> 

<%= @line_items.each do |item| %> 
    <table class="table"> 
     <thead> 
      <tr> 
       <th>Product Quantity</th> 
       <th>Product Name</th> 
       <th class="text-right">Subtotal</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <th scope="row"></th> 
       <td><%= item.order.product.name %></td> 
       <td class="text-right"></td> 
      </tr> 
     </tbody> 
    </table> 
<% end %> 
<div class="text-right"> 
    <h3><%= @order.amount %></h3> 
</div> 

orders_controller.rb

class OrdersController < ApplicationController 
    before_action :authenticate_user! 

    def index 
    @orders = Order.all 

    end 

    def show 
    @order = Order.find(params[:id]) 
    @line_items = LineItem.all 
    end 

    def new 
    @order = Order.new 
    @order.line_items.build 
    end 

    def create 
    @order = current_user.orders.build(order_params) 
    if @order.valid? 
     @order.save 
     redirect_to order_receipt_path(@order) 
    else 
     render :new 
    end 
    end 

    private 
    def order_params 
    params.require(:order).permit(line_items_attributes: [:id, :name, :product_id]) 
    end 

    def products 
    @products ||= Product.all 
    end 
    helper_method :products 
end 

回答

0

您會收到以下錯誤,因爲Order模型沒有project關聯,但LineItem了。

您可能希望通過順序控制器show方法來篩選行項目:

def show 
    @order = Order.find(params[:id]) 
    @line_items = @order.line_items 
end 

然後遍歷它們並顯示其項目名稱:

<h3>Customer - <%= @order.user.first_name %> <%= @order.user.last_name %></h3> 
<h4>Order Date - <%= @order.created_at.to_s(:date_us) %></h4> 

<%= @line_items.each do |item| %> 
    <table class="table"> 
     <thead> 
      <tr> 
       <th>Product Quantity</th> 
       <th>Product Name</th> 
       <th class="text-right">Subtotal</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <th scope="row"></th> 
       <td><%= item.product.name %></td> 
       <td class="text-right"></td> 
      </tr> 
     </tbody> 
    </table> 
<% end %> 
<div class="text-right"> 
    <h3><%= @order.amount %></h3> 
</div> 
+0

完美。我知道我錯過了一些東西。當我真的應該在控制器中處理時,我試圖在視圖中做很多事情。 –

0

秩序不知道產品。您應該致電item.product.name而不是item.order.product.name

0

還考慮急於加載產品以避免N + 1查詢問題。你可以在OrdersController#show action中這樣做:@order.line_items.includes(:product)

相關問題