# Check if row already exists?

**URL:** <https://www.ruby-forum.com/t/check-if-row-already-exists/96948>\
**Category:** Rails\
**Created:** [July 3, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/check-if-row-already-exists/96948 "2006-07-03T18:15:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [July 3, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/check-if-row-already-exists/96948/1 "2006-07-03T18:15:52Z")

</div>

I’m working on a system that has a series od “bids” associated with  
“posts”. Vurrently when a bid is placed I have it creating a new row  
each time in the “bids” table. The bids table has id, post\_id, and  
user\_id.

The logic is to check to see if a bid already exists with a given  
post\_id and user\_id. If it does, then just update the “amount” field. If  
it doesn’t, then create a new line. Here’s what I have to create the  
row:

def  
@post = Post.find(params[:post])  
@bid = Bid.new(params[:bid])  
@bid.user\_id = @session[‘user’].id  
@bid.post\_id = @post.id  
end

If I try to run a find for a bid with a user\_id matching the current  
user I get an error about not being able to find a record with that  
user\_id. That’s fine, but I want to then use that result to decide if  
I’m generating a new record or updating an existing one. Any ideas?
