routes - Rails show maximum amount of a Bid for every id product -
i'm trying print out in index view page next every product maximum amount of bid product. outcome should in index view:
banana (maximum amount banana)
table (maximum amount table)
etc
i know how print out maximum of total of products, not maximum amount of each product. being said attach code:
routes:
rails.application.routes.draw # details on dsl available within file, see http://guides.rubyonrails.org/routing.html "/new", to: "users#new" # "/index", to: "products#index" "/products", to: "products#index", as: :products "/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" "/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
bids 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) if @bid.save redirect_to product_path(@bid.product_id) else render plain: "something went wrong" end else render plain:" 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 |product| %> <p><%= link_to product.title, product_path(product.id) %></p> <p><% %></p> <% end %> <p><%= bid.maximum(:amount) %> </p> maximum amount not of every single object total of products <p><%= @products.inspect %></p> <p><%= bid.new.inspect %></p> <!-- <p><%= @bid.inspect %></p> -->
and see in browser this:
banana table tree 99999999 maximum amount not of every single object total of objects #<activerecord::relation [#<product id: 1, title: "banana", description: "this 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 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 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) create_table "bids", force: :cascade |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 |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 |t| t.string "email" t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end end
show product controller.
or use this:
@products.joins(:bids) .where("max_bid = (select max(amount) bids product_id = product.id"))
Comments
Post a Comment