Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that using a has_many :through relationship would fit well with this problem: a user can have many items through bids.</p> <p>A) Each item page would have a form that updates the bid instance for that specific item. The form would have one field: the amount of the bid the user wishes to place.</p> <p>In order to solve your first problem, each time the bid is updated you do the following:</p> <ol> <li>Get the previous user id and add to its budget the old price that the bid had (this would return their money).</li> <li>Update the bid instance with the current user_id and the newly placed bid (all this data would be passed through the form)</li> <li>Decrease the new user's budget with the bid he placed.</li> </ol> <p>B) I think that there is no need to update the item's price because keeping the item's original price is important. Thus, instead of comparing a newly placed bid with the item's price, you would compare it with the last placed bid.</p> <p>If you insist in updating the item's price, its rather easy. You would find the Item instance using your item_id stored in the bid instance and then you would simply update its price attribute with the newly placed bid.</p> <p><strong>EDIT</strong></p> <p>You can use the following code (not tested) as a starting point:</p> <p>For your Item show action, you do the following:</p> <pre><code>def show @item = Item.find(params[:id]) @bid = Bid.find_by_item_id(@item.id) end </code></pre> <p>In your show view, you would have a form like so:</p> <pre><code>&lt;%= form_for @bid do |f| &amp;&gt; &lt;%= f.label :bid_amount %&gt; &lt;%= f.text_field :amount %&gt; #etc.... &lt;%= f.submit "Submit" %&gt; &lt;% end %&gt; </code></pre> <p>This form should route your request to the Bid controller update action which should have something like this:</p> <pre><code>def update @bid = Bids.find(params[:id]) @item = Item.find(@bid.item_id) @old_user = User.find(@bid.user_id) @old_user.budget += @item.price + @bid.amount @new_user = User.find(params[:bid][:user_id]) @bid.user_id = @new_user.id @bid.amount = params[:bid][:amount] @new_user.budget -= @item.price + @bid.amount 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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