# Help with unless

**URL:** <https://www.ruby-forum.com/t/help-with-unless/74399>\
**Category:** Rails\
**Created:** [December 23, 2005, 12:03am UTC](https://www.ruby-forum.com/t/help-with-unless/74399 "2005-12-23T00:03:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [December 23, 2005, 12:03am UTC](https://www.ruby-forum.com/t/help-with-unless/74399/1 "2005-12-23T00:03:45Z")

</div>

Hi,

I need a fresh set of eyes to look at this bit of controller code and  
tell  
me why I might be getting an error message:

```
    lookup = Profit.find(:all, :conditions => [ "user_id = ? AND

```

product\_id = ?", uid, pid])  
unless lookup.empty?  
if (lookup.updated\_on \> 360.minutes.ago)  
return  
end  
end

If lookup is empty, then I get an error message that  
lookup.updated\_ondoesn’t exist. I’ve tried  
lookup.nil? but lookup gets returned in the form of [] so that empty  
works.  
But if lookup is empty should not the code inside the statement get  
ignored?

Any suggestions on how I might rework that code?

Thanks  
Steve O.  
[http://www.smarkets.net](http://www.smarkets.net)

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [December 23, 2005, 1:46am UTC](https://www.ruby-forum.com/t/help-with-unless/74399/2 "2005-12-23T01:46:17Z")

</div>

On 12/22/05, Steve O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> end
> 
> ```
> 
> If lookup is empty, then I get an error message that lookup.updated\_on  
> doesn’t exist. I’ve tried lookup.nil? but lookup gets returned in the form  
> of [] so that empty works. But if lookup is empty should not the code inside  
> the statement get ignored?
> 
> Any suggestions on how I might rework that code?

Profit.find(:all) will return an array. The array will be empty if  
none of the conditions can be satisified.

Later on, you’re calling updated\_on on the array object, and I don’t  
think that’s a valid method for an array object.

You probably want to use Profit.find(:first) (or maybe even  
Profit.find\_by\_user\_id\_and\_product\_id().

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [December 23, 2005, 3:52am UTC](https://www.ruby-forum.com/t/help-with-unless/74399/3 "2005-12-23T03:52:33Z")

</div>

Yep. You guys were both right. I had changed my call from a  
find\_by\_user\_id  
to a find(:all) and find all expects there might be more than one in the  
array.

Thanks.

Steve

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [December 23, 2005, 12:42am UTC](https://www.ruby-forum.com/t/help-with-unless/74399/4 "2005-12-23T00:42:51Z")

</div>

:all returns an array of objects. :first would retrun the object in  
question and allow you to do that. I think you want lookup[0] - the  
first object in the array.

On 12/22/05, Steve O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> Steve O.  
> [http://www.smarkets.net](http://www.smarkets.net)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
[http://brantinteractive.com](http://brantinteractive.com)  
removed\_email\_address@domain.invalid  
4034 skippack pike  
v. 267.640.2195  
f. 215.689.1454
