# Best way to check if record exists? count with limit 1?

**URL:** <https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107>\
**Category:** Rails\
**Created:** [April 16, 2007, 7:49am UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107 "2007-04-16T07:49:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 16, 2007, 7:49am UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/1 "2007-04-16T07:49:04Z")

</div>

I have a check between friends that is happening on each listing.

Right now I do

@c.friends.find(:first, :conditions=\> [“id=?”,users])

This seems kind of overkill.

For something like this, would you guys recommend:

@c.friends.count(:first, options={})

Is that the least db intensive way of doing it?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 16, 2007, 7:51am UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/2 "2007-04-16T07:51:51Z")

</div>

Woops I mean count(:limit=\>1)

Aryk G. wrote:

> I have a check between friends that is happening on each listing.
> 
> Right now I do
> 
> @c.friends.find(:first, :conditions=\> [“id=?”,users])
> 
> This seems kind of overkill.
> 
> For something like this, would you guys recommend:
> 
> @c.friends.count(:first, options={})
> 
> Is that the least db intensive way of doing it?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 16, 2007, 7:56am UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/3 "2007-04-16T07:56:32Z")

</div>

or should i just use count without the Limit 1

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 16, 2007, 9:59am UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/4 "2007-04-16T09:59:54Z")

</div>

On Apr 15, 2007, at 10:56 PM, Aryk G. wrote:

> or should i just use count without the Limit 1

Why not:

begin  
find  
rescue  
# doesn’t exist  
end

–  
– Tom M., CTO  
– Engine Y., Ruby on Rails Hosting  
– Reliability, Ease of Use, Scalability  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 16, 2007, 10:24am UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/5 "2007-04-16T10:24:58Z")

</div>

On 4/15/07, Aryk G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For something like this, would you guys recommend:
> 
> @c.friends.count(:first, options={})
> 
> Is that the least db intensive way of doing it?

@c.friends.find\_by\_id params[:friend\_id]

that will return nil if the user doesn’t exist.

If you don’t need to load the friend object into memory, sure, you  
could do a count instead of a find.

There’s really nothing intensive about this, assuming you have indexes  
setup. Considering that one user should probably only be able to be  
friends with another user once, you can create a unique index, which  
will be very zippy.

Also, be on the lookout for signs that you might want to promote a  
Friendship to its own abstraction.

Pat

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 16, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/6 "2007-04-16T18:57:13Z")

</div>

The thing is that I’m not looking up the record on its ID column, im  
looking it up on a combination of attributes.

Nevertheless, If I use find first, it will return the object into  
memory. If I use count, than it will count all the occurences.

Isn’t the best way to just test if it exists to do a count with a limit  
1?

Wouldn’t that be the least database intensive. Is there a functon right  
now to do this besides having to do Model.count(:limit=\>1,  
:conditions=\>“user\_id=4 and book\_id=7”) \> 0 ?

OR Am I just plain being to picky and should just use find :first?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 16, 2007, 7:27pm UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/7 "2007-04-16T19:27:16Z")

</div>

Hi Aryk,

Aryk G. wrote:

> Isn’t the best way to just test if it exists to do a count with  
> a limit 1?

The best way would probably be to use the exists? method. I’m not sure  
of  
the exact syntax, but for a start you might try…

answer = Model.exists?([“user\_id = ? and book\_id = ?”, 4, 7])

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 17, 2007, 12:22am UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/8 "2007-04-17T00:22:19Z")

</div>

Bill,

If you look at the code for the “exists?” function, you’ll see that its  
really just another way of writing find :first.

Which leads me to ask:

Why not just use count with limit 1 instead of find with limit 1?

I just don’t get it. It is no extra code to achieve the same results  
faster. What am I missing here?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 17, 2007, 12:59am UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/9 "2007-04-17T00:59:51Z")

</div>

Hey pat, I think I might just do that. Thanks for the response. =)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 17, 2007, 12:46am UTC](https://www.ruby-forum.com/t/best-way-to-check-if-record-exists-count-with-limit-1/92107/10 "2007-04-17T00:46:56Z")

</div>

On 4/16/07, Aryk G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Bill,
> 
> If you look at the code for the “exists?” function, you’ll see that its  
> really just another way of writing find :first.

That’s the difference between interface and implementation. You can  
look at exists? and know what it does without caring how it does it.

> Which leads me to ask:
> 
> Why not just use count with limit 1 instead of find with limit 1?
> 
> I just don’t get it. It is no extra code to achieve the same results  
> faster. What am I missing here?

Nothing. You’re just looking way too much into this. Go ahead and  
use count if you really want to. Heck, you can even override  
AR::Base.exists? to use a count implementation instead of find, if  
that’s what you want.

Pat
