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? -- Posted via http://www.ruby-forum.com/.
On Jun 10, 7:46 pm, Bruno Sousa <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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> any clues? > -- > Posted viahttp://www.ruby-forum.com/.
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 Sousa<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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}) > > any clues? > -- > Posted via http://www.ruby-forum.com/. > > > >
Thanks! -- Posted via http://www.ruby-forum.com/.
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 ]) -- Posted via http://www.ruby-forum.com/.
Bruno Sousa 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.