# Filter on variable

**URL:** <https://www.ruby-forum.com/t/filter-on-variable/64126>\
**Category:** Rails\
**Created:** [June 22, 2006, 9:36am UTC](https://www.ruby-forum.com/t/filter-on-variable/64126 "2006-06-22T09:36:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnoldng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@arnoldng](https://www.ruby-forum.com/u/arnoldng)\
**Post date:** [June 22, 2006, 9:36am UTC](https://www.ruby-forum.com/t/filter-on-variable/64126/1 "2006-06-22T09:36:53Z")

</div>

Hi all,

I have a question about filtering on a variable.  
If I have the following code in my controller to select a list of  
users

> @allusers = User.find(:all,  
> :conditions =\> [‘team\_id = ?’, @params[“id”],  
> :order =\> ‘created\_at’)

Is there an easy way to find out all users who were created one year  
ago?  
Can I do find against a variable?

Thanks.

Arnold.

---

<div class="post-metadata">

**Author:** ![arnoldng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@arnoldng](https://www.ruby-forum.com/u/arnoldng)\
**Post date:** [June 22, 2006, 11:01am UTC](https://www.ruby-forum.com/t/filter-on-variable/64126/2 "2006-06-22T11:01:51Z")

</div>

Hi arnold,

> @allusers = User.find(:all,  
> :conditions =\> [‘team\_id = ?’, @params[“id”],  
> :order =\> ‘created\_at’)

you can try adding into the sql ‘snip’((:conditions)) another condition  
in order to filter out people via a sql date function. you can read more  
info on the different type of time-functions here for your specific  
needs;  
[http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html)  
more or less it would be like

:conditions =\> [“created\_on \>= DATE\_SUB(CURDATE(),INTERVAL 30 DAY) and  
team\_id = ?”, @params[“id”]]

you should check out the link for a specific syntax that meets your  
need, but that’s the main idea…

hope it helps,

shai

---

<div class="post-metadata">

**Author:** ![arnoldng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@arnoldng](https://www.ruby-forum.com/u/arnoldng)\
**Post date:** [June 22, 2006, 11:52am UTC](https://www.ruby-forum.com/t/filter-on-variable/64126/3 "2006-06-22T11:52:51Z")

</div>

Arnold Ng \<arnoldng@…\> writes:

> 

yes, do like this.

@allusers.find { |user| user.date \< user.date-1.year }

---

<div class="post-metadata">

**Author:** ![arnoldng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@arnoldng](https://www.ruby-forum.com/u/arnoldng)\
**Post date:** [June 22, 2006, 12:26pm UTC](https://www.ruby-forum.com/t/filter-on-variable/64126/4 "2006-06-22T12:26:57Z")

</div>

Almost. You need to use find\_all or select, not find. find will return  
the first user that matches, not all of them. Also, the comparison  
“user.date \< user.date - 1.year” is wrong. Try this:

All users created in the last year

@allusers.select { |user| user.created\_at \> 1.year.ago }

Alternatively, do it using :conditions like shai suggested.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![arnoldng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@arnoldng](https://www.ruby-forum.com/u/arnoldng)\
**Post date:** [June 23, 2006, 6:40am UTC](https://www.ruby-forum.com/t/filter-on-variable/64126/5 "2006-06-23T06:40:44Z")

</div>

Thanks all, it is working perfectly.

Arnold.

Jonathan V. wrote:

> Almost. You need to use find\_all or select, not find. find will return  
> the first user that matches, not all of them. Also, the comparison  
> “user.date \< user.date - 1.year” is wrong. Try this:
> 
> All users created in the last year
> 
> @allusers.select { |user| user.created\_at \> 1.year.ago }
> 
> Alternatively, do it using :conditions like shai suggested.
> 
> -Jonathan.
