Home > Net >  RAILS - How to write a query that gets records created within 1 minute of each other
RAILS - How to write a query that gets records created within 1 minute of each other

Time:10-22

I am new to rails and am unsure how to write a query that gets records created within 1 minute of each other. Any help would be appreciated.

CodePudding user response:

you can use this query:

User.where(updated_at: (Time.now - 1.minutes)..Time.now)

In this query, you can pass time manually if you want to find records from the last 24 hours then you can use like this:

User.where(updated_at: (Time.now - 24.hours)..Time.now)

CodePudding user response:

You can group on rails level and have your records like this:

Table.all.group_by {|record| record.created_at.to_i/60}.map{|minutes, records| [minutes*60, records]}.to_h
  • Related