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. -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
Arnold Ng <arnoldng@...> writes:> > 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 > > > <at> allusers = User.find(:all, > > :conditions => [''team_id = ?'', <at> params["id"], > > :order => ''created_at'') >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. On 6/22/06, chenge <chenge2k@gmail.com> wrote:> yes, do like this. > > @allusers.find { |user| user.date < user.date-1.year } >
Thanks all, it is working perfectly. Arnold. Jonathan Viney 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.-- Posted via http://www.ruby-forum.com/.