Rails 显示每个 id 产品的最大出价
Rails show maximum amount of a Bid for every id product
我试图在每个产品旁边的索引视图页面中打印出该产品的最高出价。所以索引视图中的结果应该是这样的:
香蕉(香蕉的最大量)
Table(table 的最大金额)
等等
我知道如何打印出所有产品总和的最大值,但不知道每个产品的最大数量。话虽如此,我附上了我的代码:
路线:
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)
if @bid.save
redirect_to product_path(@bid.product_id)
else
render plain: "something went wrong"
end
else
render plain:" Too late"
end
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)
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
显示您的产品控制器。
或者使用这样的东西:
@products.joins(:bids)
.where("max_bid = (select max(amount) from bids where product_id = product.id"))
实际上这是 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> -->
现在可以了:)
我试图在每个产品旁边的索引视图页面中打印出该产品的最高出价。所以索引视图中的结果应该是这样的:
香蕉(香蕉的最大量)
Table(table 的最大金额)
等等
我知道如何打印出所有产品总和的最大值,但不知道每个产品的最大数量。话虽如此,我附上了我的代码:
路线:
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)
if @bid.save
redirect_to product_path(@bid.product_id)
else
render plain: "something went wrong"
end
else
render plain:" Too late"
end
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)
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
显示您的产品控制器。
或者使用这样的东西:
@products.joins(:bids)
.where("max_bid = (select max(amount) from bids where product_id = product.id"))
实际上这是 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> -->
现在可以了:)