Note that there are some explanatory texts on larger screens.

plurals
  1. POAuction site in rails and working with bids
    text
    copied!<p>I need some assistance in working with the bids on this app. When a user register they get to set their budget (which is an integer in the schema). Users are then able to create an item with a set price (which is also an integer in the schema for the items table). </p> <p>Im trying to figure out when a user creates a bid on the item </p> <p>A) How do I decrease the users current budget if the highest bidder, but if gets out bid, the money returns to their budget.</p> <p>B) Increase the items "price" that has been set with the current highest bid.</p> <p>heres a visual of the schema:</p> <pre><code> create_table "bids", force: true do |t| t.datetime "created_at" t.datetime "updated_at" t.integer "item_id" t.integer "user_id" t.integer "amount" end create_table "items", force: true do |t| t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" t.string "title" t.integer "price" end create_table "users", force: true do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0 t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" t.integer "budget" end </code></pre> <p>Bids controller:</p> <pre><code>class BidsController &lt; ApplicationController before_action :set_bid, only: [:show, :edit, :update, :destroy] def show @bid = Bid.find(params[:id]) end def new @item = Item.find(params[:item_id]) @bid = @item.bids.build end def create @item = Item.find(params[:item_id]) @bid = @item.bids.new(bid_params) respond_to do |format| if @bid.save format.html { redirect_to @item, notice: 'Your item has been updated.'} else format.html { render action: 'new' } end end end private def set_bid @bid = Bid.find(params[:id]) end def bid_params params[:bid][:user_id] = current_user.id params[:bid].permit(:amount, :user_id, :item_id) end end </code></pre> <p>Items controller:</p> <pre><code>class ItemsController &lt; ApplicationController before_action :set_item, only: [:show, :edit, :update, :destroy] before_filter :user_signed_in? def index @item = Item.all end def show @item = Item.find(params[:id]) end def edit end def new @item = Item.new @item.bids.build end def create @item = Item.new(item_params) if @item.save redirect_to @item, notice: 'Item successfully created.' else format.html { render action: 'new'} end end def update respond_to do |format| if @item.update(item_params) format.html { redirect_to @item, notice: 'Your item has been updated.'} else format.html { render action: 'edit'} end end end def destroy @item.destroy respond_to do |format| format.html { redirect_to items_url } end end private def set_item @item = Item.find(params[:id]) end def item_params params[:item][:user_id] = current_user.id params[:item].permit(:price, :user_id, :title, :bids_attributes =&gt; [:amount, :user_id, :item_id]) end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload