2016-11-13 2 views
0

Ich versuche, in der Indexansicht Seite neben jedem Produkt die maximale Höhe des Gebots für dieses Produkt auszudrucken. So sollte das Ergebnis wie dies in der Indexansicht sein:Rails zeigen maximale Menge eines Gebots für jedes ID-Produkt

Banana (Höchstbetrag für Bananen)
Tabelle (Höchstbetrag für Tabelle)
etc

Ich weiß, wie man aus dem Maximum der Gesamt drucken aller Produkte, aber nicht die maximale Menge jedes Produkts. Davon abgesehen lege ich meinen Code:

WEGE:

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

Gebote Controller:

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> -->

Und was ich im Browser zu sehen, ist dies:

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>

SCHEMA:

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

Antwort

0

Eigentlich ist dies die richtige Antwort in der INDEX.HTML.ERB Datei:

<% @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> -->

Und jetzt funktioniert es:)

0

Zeigen Sie Ihren Produktcontroller.

Oder verwenden Sie so etwas wie dieses:

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

Ich habe die Antwort bekommen! Dein klingt für mich komplexer. Danke trotzdem! – Defoe

Verwandte Themen