# Search again using previous records found

**URL:** <https://www.ruby-forum.com/t/search-again-using-previous-records-found/111107>\
**Category:** Rails\
**Created:** [August 2, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/search-again-using-previous-records-found/111107 "2007-08-02T18:51:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cfingerh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@cfingerh](https://www.ruby-forum.com/u/cfingerh)\
**Post date:** [August 2, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/search-again-using-previous-records-found/111107/1 "2007-08-02T18:51:40Z")

</div>

I have a companies table and a products table  
companies --\> id,name,address,owner,country …  
products --\> id, company\_id,name, photo

In my controller I have

@companies=Company.find(:all,:include=\>‘products’,:conditions=\>["products.name  
LIKE '%tool% and company.country=‘spain’ "])

in my view i can show all the companies that have the product

\<% for company in @companies %\>  
\<%= company.name %\>  
\<%end%\>

and I can show all the products found  
\<% for product in @companies.products %\>  
\<%= product.name %\>  
\<%end%\>

That’s all OK. But now I want to research the list of products under a  
new criteria using the previous records found.  
@companies.products.find(:all,:conditions=\>[‘date\>2007-07-05’])

But I get ALL the product with criteria date\>2007-07-05 and not just  
those who meet both criterias (date criteria and name like ‘%tool%’)  
based on the records found previously. How can I do that?

---

<div class="post-metadata">

**Author:** ![cfingerh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@cfingerh](https://www.ruby-forum.com/u/cfingerh)\
**Post date:** [August 3, 2007, 8:07am UTC](https://www.ruby-forum.com/t/search-again-using-previous-records-found/111107/2 "2007-08-03T08:07:06Z")

</div>

Hi,

2007/8/2, cfingerh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> That’s all OK. But now I want to research the list of products under a  
> new criteria using the previous records found.  
> @companies.products.find(:all,:conditions=\>[‘date\>2007-07-05’])
> 
> But I get ALL the product with criteria date\>2007-07-05 and not just  
> those who meet both criterias (date criteria and name like ‘%tool%’)  
> based on the records found previously. How can I do that?

You could use select on @companies.products:

@companies.products.select {|product| product.date \> Time.mktime(2007,  
7, 5) }

Lutz

---

<div class="post-metadata">

**Author:** ![cfingerh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@cfingerh](https://www.ruby-forum.com/u/cfingerh)\
**Post date:** [August 6, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/search-again-using-previous-records-found/111107/3 "2007-08-06T22:23:20Z")

</div>

Thank you very much. And if I want to get the cheapest product with  
those restrictions?

@companies.products.select {|product| product.date \>  
Time.mktime(2007, 7, 5) }

But I want to get the product with the cheapest price ----\>  
product.price
