mysql - ActiveRecord scope to find records created today? -
this has got easy thing do, think rail's timezone implementation throwing off work...
how can create scope in rails 3 (and ruby 1.9.2) query records created today?
currently, i'm doing this:
scope :today, lambda { where("created_at >= ? , created_at <= ?", date.today.beginning_of_day, date.today.end_of_day) }
and doesn't appear working should. want "today" represent 24 hour period 12am 11:59pm user's local timezone. need convert date utc or something?
you need consider user's timezone. is, beginning of day , end of day in user's timezone. that, first parse current time in user's timezone. can set zone user's timezone , calculation or can parse time directly using variation of following code.
timezone = current_user.timezone # mountain time (us & canada) users_current_time = activesupport::timezone[timezone].parse(time.now.to_s)
users_current_time.beginning_of_day
, users_current_time.end_of_day
should generate right time range (which app convert utc , fire query, if utc applications timezone)
if want scopes only, suggest set time.zone = current_user.timezone
before_filter
of actions , use in scope. this railscast gives fair idea how go doing that.
Comments
Post a Comment