2016-11-13 63 views
0

我試圖在每個產品旁邊的索引視圖頁中打印出該產品的最高出價量。所以結局應該是這樣的索引視圖:Rails顯示每個id產品的最高出價量

香蕉(香蕉的最高金額)
表(用於表最高金額)

我知道如何打印出最大的總的的所有產品,但不是每個產品的最大數量。話雖這麼說,我附上我的代碼:

途徑:

Rails.application.routes.draw do 
 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
 

 
    get "/new", to: "users#new" 
 
    # get "/index", to: "products#index" 
 
    get "/products", to: "products#index", as: :products 
 

 
    get "/new_products", to: "products#new" 
 
    # el form_for siempre necesitará un path en forma de as: ... ya que no le sirve solo la url 
 
    post "/products", to: "products#create" 
 
    get "/products/show/:id", to: "products#show", as: :product 
 
    post "/bids/new_bid", to: "bids#create" 
 
    # post "/new_bid", to: "bids#create" 
 
    post "/products", to: "bids#pass_bid_amount" 
 

 
end

投標控制器:

class BidsController < ApplicationController 
 
    
 
    def create 
 
    user= User.find_by(email: params[:email]) 
 
    if Time.now < params[:product_deadline] 
 
     @bid= Bid.new(amount: params[:amount].to_i, user_id: user.id.to_i, product_id: params[:product_id].to_i) \t \t \t \t 
 
     if @bid.save 
 
     redirect_to product_path(@bid.product_id) 
 
     else 
 
     render plain: "something went wrong" 
 
     end 
 
    else 
 
     render plain:" Too late" 
 
    end \t 
 
    end 
 

 
    def pass_bid_amount 
 
    @bid= Bid.new(amount: params[:amount].to_i, user_id: user.id.to_i, product_id: params[:product_id].to_i) 
 
    @bid.save 
 
    render "index" 
 
    # redirect_to products_path(@bid.product_id) \t \t 
 
    end 
 

 
end

INDEX.HTML.ERB:

<% @products.each do |product| %> 
 
    <p><%= link_to product.title, product_path(product.id) %></p> 
 
    <p><% %></p> 
 
<% end %> 
 

 
<p><%= Bid.maximum(:amount) %> </p> 
 

 
This maximum amount is not of every single object but the total of all products 
 

 
<p><%= @products.inspect %></p> 
 
<p><%= Bid.new.inspect %></p> 
 
<!-- <p><%= @bid.inspect %></p> -->

而且我在瀏覽器中看到的是:

banana 
 

 
table 
 

 
tree 
 

 
99999999 
 

 
This maximum amount is not of every single object but the total of all objects 
 

 
#<ActiveRecord::Relation [#<Product id: 1, title: "banana", description: "this is a fruit", user_id: nil, deadline: "2016-11-22 00:00:00", created_at: "2016-11-12 12:40:01", updated_at: "2016-11-12 12:40:01">, #<Product id: 2, title: "table", description: "this is an object", user_id: nil, deadline: "2016-11-22 00:00:00", created_at: "2016-11-12 12:40:01", updated_at: "2016-11-12 12:40:01">, #<Product id: 3, title: "tree", description: "this is a tree", user_id: nil, deadline: "2016-11-22 00:00:00", created_at: "2016-11-12 12:40:01", updated_at: "2016-11-12 12:40:01">]> 
 

 
#<Bid id: nil, amount: nil, user_id: nil, product_id: nil, created_at: nil, updated_at: nil>

架構:

ActiveRecord::Schema.define(version: 20161109151534) do 
 

 
    create_table "bids", force: :cascade do |t| 
 
    t.integer "amount" 
 
    t.integer "user_id" 
 
    t.integer "product_id" 
 
    t.datetime "created_at", null: false 
 
    t.datetime "updated_at", null: false 
 
    t.index ["product_id"], name: "index_bids_on_product_id" 
 
    t.index ["user_id"], name: "index_bids_on_user_id" 
 
    end 
 

 
    create_table "products", force: :cascade do |t| 
 
    t.string "title" 
 
    t.string "description" 
 
    t.integer "user_id" 
 
    t.datetime "deadline" 
 
    t.datetime "created_at", null: false 
 
    t.datetime "updated_at", null: false 
 
    t.index ["user_id"], name: "index_products_on_user_id" 
 
    end 
 

 
    create_table "users", force: :cascade do |t| 
 
    t.string "email" 
 
    t.string "name" 
 
    t.datetime "created_at", null: false 
 
    t.datetime "updated_at", null: false 
 
    end 
 

 
end

回答

0

其實這是在INDEX.HTML.ERB文件的正確響應:

<% @products.each do |product| %> 
 

 

 

 

 
<p><%= link_to product.title, product_path(product.id) %> 
 
<% maximum_bid = product.bids.max_by {|bid| bid.amount } %> 
 
<p><%= maximum_bid.amount%></p> 
 

 

 

 
</p> 
 
<p><% %></p> 
 
<% end %> 
 

 
<!-- <p><%= Bid.maximum(:amount) %> </p> --> 
 

 

 

 

 
<p><%= @products.inspect %></p> 
 
<p><%= Bid.new.inspect %></p> 
 
<!-- <p><%= @bid.inspect %></p> -->

而現在它的工作原理: )

0

顯示您的產品控制器。

還是用這樣的:

@products.joins(:bids) 
    .where("max_bid = (select max(amount) from bids where product_id = product.id")) 
+0

我得到了答案!你的聲音對我來說更加複雜。不管怎麼說,還是要謝謝你! – Defoe

相關問題