Graeme
Graeme

Reputation: 497

ruby OR statement executes twice when both variables are true

I have a simple statement that executes if at least one of two variables are true. The problem I am having is if both variables are true the statement is executed twice. I only need it to execute once.

<% if intensity == "true" || @planned_intensity == "true" %>
  <div class="red_dot"></div>
<% end %>

Am I missing something obvious here? In the case where both variables are true I get two red dots.

//edit

It appears to be somewhere else but I cant seem to find the issue, here is the entire block:

<% LogEntry.cal_entry(entry_date.to_date, @athlete.id).each do |entry| -%>
  <% workouts = entry.workouts -%>
  <% workouts.each do |workout| -%>
    <% @planned_intensity = false %>
    <% planned_workouts = workout.planned_workouts -%>
    <% planned_workouts.each do |pworkout| -%>
      <% @planned_times = pworkout.planned_workout_times -%>
      <% @planned_times.each do |planned_time| -%>
        <% if planned_time.zone > 1 %>
          <% @planned_intensity = true %>
        <% end %>
      <% end %>
    <% end %>

  <% completed_workouts = workout.completed_workouts -%>
  <% completed_workouts.each do |completed_workout| -%>
    <li id="<%= workout.id.to_s() %>">
    <% completed_workout.workout_times.each do |time| -%>
      <% intensity = "true" %>
      <% if time.zone > 1 %>
        <% intensity = true %>
      <% else %>
        <% intensity = false %>
      <% end %>
      <% if intensity == "true" || @planned_intensity == true %>
          <div class="red_dot"></div>
        <% end %>
    <% end %>
      <%= link_to_coaches_dialog workout.activity_id, entry.id.to_s(), "edit", "cal", @athlete.id.to_s, workout.id.to_s(), entry_date.to_date.to_s() %>
      <% total_duration = 0 %>
      <% completed_workout.workout_times.each do |workout_time| -%>
        <% total_duration += workout_time.duration %>
      <% end -%>
      <% total_planned_duration = 0 %>
      <% @planned_times.each do |planned_time| %>
        <% total_planned_duration += planned_time.duration %>
      <% end %>
      <%= time_cleanup(total_duration/60) %>
      <% unless total_planned_duration == 0 %>
      <span class="smaller"> / 
      <%= time_cleanup(total_planned_duration/60) %></span>
      <% end %>
    </li>
  <% end -%>
  <% end -%>
<% end -%>

Upvotes: 0

Views: 137

Answers (1)

Matt Gibson
Matt Gibson

Reputation: 14959

It's hard to tell from looking at this exactly what could be causing the red dot to appear twice. I'd imagine the containing loop is running twice, but we have no idea what data is in the workout_times.

The real problem is that you are setting the values for intensity and @planned_intensity in this view code. You ought to really be doing logic like this in the model or controller. This layer (HTML view templates) is just for presentation. The reason for the separation is so that you don't get hard to pinpoint bugs like this one :)

I'd refactor so that the view calls a method on a workout_time object, so that the logic is then in this method. At least then you'll know where to look. You can also unit test this method, whereas testing this view will be awkward.

What models do you have right now? I'd imagine you should have workout and workout_time, then add completed? and planned? methods on the workout model and a has_intensity? method on workout_time.

Upvotes: 2

Related Questions