Filter on variable

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.

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
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

Arnold Ng <[email protected]…> writes:

yes, do like this.

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

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs