Tuesday, 27 August 2013

How to properly use time zone in Rails

How to properly use time zone in Rails

The logic: I want to show placeholder exercises if no log entries for
today exists. If log entries for today exists, then don't show placeholder
exercises even if they exist.
.log-entries
- unless exercise.log_entries.for_user(workout.user).blank? ||
exercise.last_log_date(workout.user) ==
Time.zone.today.beginning_of_day
%p Nice job performing this #{timeago
exercise.last_log_date(workout.user)}. Try going up in
difficulty this time.
= exercise.last_log_date(workout.user).to_date
= exercise.last_log_entry(workout.user).created_at.to_date
%br
= Time.current.to_date
%br
- for log_entry in exercise.placeholder_log_entries!(workout.user)
= render 'log_entries/form', log_entry: log_entry, exercise:
exercise
- for log_entry in
exercise.log_entries.for_today.order("created_at
ASC").for_user(workout.user) do
= render 'log_entries/form', log_entry: log_entry, exercise:
exercise
- if exercise.log_entries.blank?
%p.hint.not-performed-yet You haven't performed this exercise yet.
= link_to exercise.render_add_text,
user_workout_exercise_log_entries_path(workout, workout.user,
exercise), class: "button radius", remote: true, method: :post
Whats driving me nuts... is that I recorded log entries for today, yet
placeholder exercises are still showing up. So that first unless is
passing.
Upon debugging I'll get values like this:
exercise last log date: 2013-08-26 00:00:00 -0700
today: 2013-08-27 00:00:00 -0700
But that exer last log date is actually recorded today, just right now!
Why is it a day behind?
#<LogEntry id: 226, reps: 8, weight: 55, effort: nil, description: nil,
loggable_id: 3, loggable_type: "Exercise", created_at: "2013-08-27
06:00:12", updated_at: "2013-08-28 03:06:56", user_id: 1, rest_after_set:
nil, difficulty: nil, distance: nil, duration: nil, measurement: nil,
placeholder: false>
Created at is 2013-08-27!!! Why is it rendering 26 in the view? I'm losing
my mind.

No comments:

Post a Comment