# How know record was found?

**URL:** <https://www.ruby-forum.com/t/how-know-record-was-found/119000>\
**Category:** Rails\
**Created:** [November 2, 2007, 1:33am UTC](https://www.ruby-forum.com/t/how-know-record-was-found/119000 "2007-11-02T01:33:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 2, 2007, 1:33am UTC](https://www.ruby-forum.com/t/how-know-record-was-found/119000/1 "2007-11-02T01:33:58Z")

</div>

OK, for the life of me I can’t see anywhere how I am supposed to  
figure out if a record was actually found.

class Shape \< ActiveRecord::Base

```
 def self.findSquare
     find(:first, conditions: whetever.......)

     # HOW DO AI KNOW ONE WAS ACTUALLY FOUND?
     # SAME FOR find :all ??

 end

```

end

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 2, 2007, 1:45am UTC](https://www.ruby-forum.com/t/how-know-record-was-found/119000/2 "2007-11-02T01:45:47Z")

</div>

Greg W. wrote:

> OK, for the life of me I can’t see anywhere how I am supposed to  
> figure out if a record was actually found.
> 
> class Shape \< ActiveRecord::Base
> 
> ```
> def self.findSquare
> find(:first, conditions: whetever.......)
> 
> # HOW DO AI KNOW ONE WAS ACTUALLY FOUND?
> # SAME FOR find :all ??
> 
> end
> 
> ```
> 
> end
> 
> – gw

class Shape \< ActiveRecord::Base

```
 def self.findSquare
     results = find(:first, conditions: whetever.......)
     if results.length > 0
       results
     else
       raise "No hits!"
     end
 end

```

end

Is this what you want?

Semin

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 2, 2007, 2:21am UTC](https://www.ruby-forum.com/t/how-know-record-was-found/119000/3 "2007-11-02T02:21:18Z")

</div>

On Nov 1, 2007, at 5:45 PM, Semin Lee wrote:

> > ```
> > # SAME FOR find :all ??
> > 
> > ```
> 
> ```
> raise "No hits!"
> end
> end
> 
> ```
> 
> end
> 
> Is this what you want?

Length is not an available method, nor size, nor count, nor anything  
else that would seem logical or semi-logical.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 2, 2007, 2:23am UTC](https://www.ruby-forum.com/t/how-know-record-was-found/119000/4 "2007-11-02T02:23:04Z")

</div>

On Nov 1, 2007, at 8:45 PM, Semin Lee wrote:

> > ```
> > # SAME FOR find :all ??
> > 
> > ```
> 
> ```
> if results.length > 0
> results
> else
> raise "No hits!"
> end
> end
> 
> ```
> 
> end
> 
> Is this what you want?
> 
> Semin

Well, not quite. find(:first) is going to be nil or a Shape object.  
If it’s nil, you can’t call nil.length and if it’s a Shape object,  
then Shape#length might actually be something else.

if result = find(:first, :conditions =\> …)

# do something with the record in result

end

result\_array = find(:all, :conditions =\> …)  
unless result\_array.empty?

# do something with the records in the result\_array

end

for either case, you might just return the find() and let the caller  
check for nil or iterate over the empty array

(Even if find(:all, …) only gets a single match, it will still be in  
an array.)

-Rob  
Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 2, 2007, 2:54am UTC](https://www.ruby-forum.com/t/how-know-record-was-found/119000/5 "2007-11-02T02:54:02Z")

</div>

And if there are no matches, it will be an empty array. If you need  
to do something with it, try this:

Blah.find(:all).each do |blah|  
# Do something  
end

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 2, 2007, 3:01am UTC](https://www.ruby-forum.com/t/how-know-record-was-found/119000/6 "2007-11-02T03:01:57Z")

</div>

Rob B. wrote:

> On Nov 1, 2007, at 8:45 PM, Semin Lee wrote:
> 
> > > ```
> > > # SAME FOR find :all ??
> > > 
> > > ```
> > 
> > ```
> > if results.length > 0
> > results
> > else
> > raise "No hits!"
> > end
> > end
> > 
> > ```
> > 
> > end
> > 
> > Is this what you want?
> > 
> > Semin
> 
> Well, not quite. find(:first) is going to be nil or a Shape object.  
> If it’s nil, you can’t call nil.length and if it’s a Shape object,  
> then Shape#length might actually be something else.
> 
> if result = find(:first, :conditions =\> …)
> 
> # do something with the record in result
> 
> end
> 
> result\_array = find(:all, :conditions =\> …)  
> unless result\_array.empty?
> 
> # do something with the records in the result\_array
> 
> end
> 
> for either case, you might just return the find() and let the caller  
> check for nil or iterate over the empty array
> 
> (Even if find(:all, …) only gets a single match, it will still be in  
> an array.)
> 
> -Rob  
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

oops, sorry I misunderstood the return types of find(:all) and  
find(:first).

thanks Rob,

Semin

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 2, 2007, 5:23am UTC](https://www.ruby-forum.com/t/how-know-record-was-found/119000/7 "2007-11-02T05:23:02Z")

</div>

On Nov 1, 2007, at 6:21 PM, Rob B. wrote:

> > ```
> > end
> > 
> > ```
> > 
> > end

> # do something with the records in the result\_array
> 
> end
> 
> for either case, you might just return the find() and let the caller  
> check for nil or iterate over the empty array
> 
> (Even if find(:all, …) only gets a single match, it will still be in  
> an array.)

I noodled with something like that but ran into problems with  
subsequent steps which got me sidetracked. I now have that sorted  
out. I apparenty had my head warped by the combination of what Rails  
was doing, Ruby’s implied return, and the class vs instance scoping  
of methods and my own arbitrary attributes added to the model.

My son managed to unwarp all that, and now I can see again.

Thx.

– gw
