如何计算当前级别的miss_days? [英] How to count current level missed_days?

查看:174
本文介绍了如何计算当前级别的miss_days?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何调用类似<%= habit.current_level.missed_days%> 这样的地方我们只调用 missed_days 来自 current_level ,而不是所有 missed_days 的习惯(只是给出一个大概的概念)我们想要什么)。

How can we call something like this <%= habit.current_level.missed_days %> where we only call the missed_days from the current_level, instead of all the missed_days in a habit (to just give an general idea of what we want).

例如,如果选中两个方框,则调用<%= habit.missed_days%> 在习惯索引中将显示 2 ,如果选中8个框,它将显示 8 ,但此处的目标是是即使检查了8个盒子:

For example if two boxes are checked, calling <%= habit.missed_days %> in the habits index will show 2 and if eight boxes are checked it will show 8, but the goal here is that even if 8 boxes are checked:

它仍然只会说 2 指数中的罢工,因为我们正在尝试只计算当前级别的 missed_days

it will still only say 2 strikes in the index because we are trying to only count the missed_days from the current level.

class Habit < ActiveRecord::Base
    belongs_to :user
    has_many :comments, as: :commentable
    has_many :levels
    serialize :committed, Array
    validates :date_started, presence: true
    before_save :current_level
    acts_as_taggable
    scope :private_submit, -> { where(private_submit: true) }
    scope :public_submit, -> { where(private_submit: false) }

attr_accessor :missed_one, :missed_two, :missed_three

    def save_with_current_level
        self.levels.build
        self.levels.build
        self.levels.build
        self.levels.build
        self.levels.build
        self.save
    end

    def self.committed_for_today
    today_name = Date::DAYNAMES[Date.today.wday].downcase
    ids = all.select { |h| h.committed.include? today_name }.map(&:id)
    where(id: ids)
  end 

    def current_level
            return 0 unless date_started
            committed_wdays = committed.map { |day| Date::DAYNAMES.index(day.titleize) }
            n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday } - self.missed_days

      case n_days     
          when 0..9
            1
          when 10..24
            2
          when 25..44
            3
          when 45..69
            4
          when 70..99
            5
          else
            "Mastery"
        end
    end
end

我们有 t.integermissed_days,默认值:0 在等级&习惯表。

We have t.integer "missed_days", default: 0 in the levels & habits tables.

大部分 missed_days 逻辑可追溯到其控制器:

Much of missed_days logic can be traced to its controller:

class DaysMissedController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]

  def create
    habit = Habit.find(params[:habit_id])
    habit.missed_days = habit.missed_days + 1
    habit.save!
    level = habit.levels.find(params[:level_id])
    level.missed_days = level.missed_days + 1
    level.save!
    head :ok # this returns an empty response with a 200 success status code
  end

  def destroy
    habit = Habit.find(params[:habit_id])
    habit.missed_days = habit.missed_days - 1
    habit.save!
    level = habit.levels.find(params[:level_id])
    level.missed_days = level.missed_days - 1
    level.save!
    head :ok # this returns an empty response with a 200 success status code
  end
end

要点 https:// gist.github.com/RallyWithGalli/c66dee6dfb9ab5d338c2

感谢您的精彩!

推荐答案

确切地看到你想要做的事情有点棘手,但希望这会指出你正确的方向:

It's a bit tricky to see exactly what you're trying to do but hopefully this will point you in the right direction:

它看起来你在 Habit 模型上有一个 current_level 方法,但此时它会返回当前级别数字而不是当前级别模型

It looks like you have a current_level method on the Habit model but at the moment this returns the current level number and not the current level model.

因此可能将此方法重命名为 current_level_number 然后添加 current_level 方法,如:

So maybe rename this method to current_level_number and then add a current_level method like:

def current_level
  levels[current_level_number - 1] # remember array indexes start at 0
end

然后你应该可以 habit.current_level.missed_days 获取当前级别的错过天数。

then you should be able to do habit.current_level.missed_days to get the missed days for the current level.

此外,请小心 current_level 方法。它在大多数情况下返回一个数字,即1到5,但在特定情况下返回一个字符串Mastery。我返回不同类型的方法可能会给你带来麻烦。

Also, be careful with your current_level method. It returns a number i.e. 1 to 5 in most cases but then returns a string "Mastery" under specific circumstances. I method that returns different types like this is likely to cause you trouble.

更详细一点:你什么时候创造了一个习惯,看起来你已经设置了5个级别:

A bit more detail: When you've created the habit it looks like you've set it up with 5 levels:

def save_with_current_level
  self.levels.build
  self.levels.build
...

所以这些将是级别[0] 级别[1] 级别[4]

在你的 DaysMissedController 中,你正在更新那些习惯本身和一个水平所以我假设你想要获得当前水平的 days_missed 计数。

In your DaysMissedController you're updating the days missed count on the habit itself and on one of the levels so I am assuming that it's this days_missed count for the current level that you want to get hold of.

你的 current_level 方法根据 n_days 返回介于1和5之间的级别数,所以当用户处于1级时我们想要的习惯 levels [0] ,对于2级,级别[1] 等等。

Your current_level method returns a level number between 1 and 5 based on n_days so when the user is on level 1 for the habit we want levels[0], for level 2, levels[1] and so on.

这篇关于如何计算当前级别的miss_days?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆