# Undefined method empty? when find first invoked on db

**URL:** <https://www.ruby-forum.com/t/undefined-method-empty-when-find-first-invoked-on-db/172140>\
**Category:** Rails\
**Created:** [July 15, 2009, 3:49pm UTC](https://www.ruby-forum.com/t/undefined-method-empty-when-find-first-invoked-on-db/172140 "2009-07-15T15:49:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 15, 2009, 3:49pm UTC](https://www.ruby-forum.com/t/undefined-method-empty-when-find-first-invoked-on-db/172140/1 "2009-07-15T15:49:11Z")

</div>

if model.compiled\_this\_week.first.empty?  
OR  
if model.compiled\_this\_week.find(:first).empty?

… return undefined method empty?

if model.compiled\_this\_week.all.empty?  
OR  
if model.compiled\_this\_week.find(:all).empty?

… work as expected

compiled\_this\_week is a named scope applied to specific date/search  
conditions.

named\_scope :compiled\_this\_week, lambda { { :conditions =\> ['created\_at

> ? and created\_at \< ?’, Time.now.beginning\_of\_week,  
> Time.now.end\_of\_week] } }

Any reason why this returns undefined method?

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 15, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/undefined-method-empty-when-find-first-invoked-on-db/172140/2 "2009-07-15T16:01:31Z")

</div>

Ah that makes sense now - thanks for the clarification on that Craig!

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 15, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/undefined-method-empty-when-find-first-invoked-on-db/172140/3 "2009-07-15T15:54:34Z")

</div>

As a named\_scope, compiled\_this\_week will return a collection, even if  
it  
finds only one record matching its criteria. On a collection, #empty?  
will  
work. When you call #first or #find(:first) on the collection returned  
by  
compiled\_this\_week, you get the first record. #empty? is undefined on  
that  
record.  
Regards,  
Craig

–  
Craig D.  
Mutually Human Software

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com/)**
>
> Technology Made for Humans by humans Data Analytics Data Analytics Data has become a critical part of business for most organizations. We work with you to ensure you have confidence in your data, can properly access it, and present it in a way that...
