Compare timestamps

Hi everyone,
‘Person’ migration includes timestamps.
I want to find every person created ‘today’.

Tried the command below, but I think it compares hour,minute and seconds
too. I just want to compare day,month and year.
Person.find(:all, :conditions => {:created_at => Time.now})

any clues?

Person.all( :conditions => [ ‘created_at BETWEEN ? AND ?’,
Time.now.at_beginning_of_day, Time.now.at_beginning_of_day + 1.day ] )

Maurício Linhares
http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr

On Wed, Jun 10, 2009 at 3:46 PM, Bruno

On Jun 10, 7:46 pm, Bruno S. [email protected]
wrote:

Hi everyone,
‘Person’ migration includes timestamps.
I want to find every person created ‘today’.

Tried the command below, but I think it compares hour,minute and seconds
too. I just want to compare day,month and year.
Person.find(:all, :conditions => {:created_at => Time.now})

You can’t use the hash form of :conditions for this. You need to ask
the database for those records with a created_at in a certain range
(exactly what range will depend on your definiiton of today: past 24
hours or since midnight)

Fred

Thanks!

Okay,
another issue:
There are persons with the same names. I need to group them by their
names and add their age(the ones that have same name).

this table:

NAME | AGE
John | 23
Abe | 45
Amy | 40
John | 7
John | 5
Amy | 5

would turn into

NAME | AGE
John | 35
Abe | 45
Amy | 45

Tried:
Person.find(:all, :group => “name”, :conditions => [ ‘created_at BETWEEN
? AND ?’, Time.now.at_beginning_of_day, Time.now.at_beginning_of_day +
1.day ])

Thanks everyone!

Bruno S. wrote:

Okay,
another issue:
There are persons with the same names. I need to group them by their
names and add their age(the ones that have same name).
[…]

This is best done with SQL aggregate functions, which ActiveRecord
abstracts as Calculations. From memory, you probably want something
like

Person.sum(:age, :group => :name)

…but check the docs, 'cause I may have messed up the syntax.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]