如何计算当前等级missed_days?
How to count current level missed_days?
我们如何调用这样的东西 <%= habit.current_level.missed_days %>
我们只调用 current_level
中的 missed_days
,而不是习惯中的所有 missed_days
(只是大致了解我们想要什么)。
比如勾选两个框,在习惯索引中调用<%= habit.missed_days %>
会显示2
,勾选八个框会显示8
,但是这里的目标是即使勾选了 8 个框:
它仍然只会在指数中显示 2
次罢工,因为我们试图 仅计算当前水平的 missed_days
次。
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.integer "missed_days", default: 0
。
大部分missed_days
逻辑都可以追溯到它的控制器:
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
谢谢你的精彩!
要准确了解您要做什么有点棘手,但希望这会为您指明正确的方向:
看起来你在 Habit
模型上有一个 current_level
方法,但目前这个 returns 当前级别 number 和不是当前级别 model.
所以可以将此方法重命名为 current_level_number
,然后添加一个 current_level
方法,例如:
def current_level
levels[current_level_number - 1] # remember array indexes start at 0
end
那么您应该可以habit.current_level.missed_days
获取当前级别的错过天数。
此外,请注意您的 current_level
方法。在大多数情况下,它 returns 一个数字,即 1 到 5,但在特定情况下 returns 一个字符串 "Mastery"。我想 returns 像这样的不同类型可能会给你带来麻烦。
更多细节:当您养成习惯时,您似乎已经设置了 5 个级别:
def save_with_current_level
self.levels.build
self.levels.build
...
所以这些将是 levels[0]
、levels[1]
到 levels[4]
在您的 DaysMissedController
中,您正在更新习惯本身和其中一个级别的错过天数,所以我假设这是您想要的当前级别的 days_missed
计数抓住。
您的 current_level
方法 returns 基于 n_days
的 1 到 5 之间的级别数字,因此当用户处于我们想要的习惯的级别 1 时 levels[0]
,对于 2 级,levels[1]
依此类推。
这只是一个假设,因为你没有提到很多关联,我什至不确定我是否理解你想要什么,但如果我的想法是正确的,我会 think/assume它会像这样
MissedDay.joins(level: {habit: :user})
.merge(User.where(id: user_id))
.merge(Habbit.where(id: habbit_id))
.count
我们如何调用这样的东西 <%= habit.current_level.missed_days %>
我们只调用 current_level
中的 missed_days
,而不是习惯中的所有 missed_days
(只是大致了解我们想要什么)。
比如勾选两个框,在习惯索引中调用<%= habit.missed_days %>
会显示2
,勾选八个框会显示8
,但是这里的目标是即使勾选了 8 个框:
它仍然只会在指数中显示 2
次罢工,因为我们试图 仅计算当前水平的 missed_days
次。
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.integer "missed_days", default: 0
。
大部分missed_days
逻辑都可以追溯到它的控制器:
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
谢谢你的精彩!
要准确了解您要做什么有点棘手,但希望这会为您指明正确的方向:
看起来你在 Habit
模型上有一个 current_level
方法,但目前这个 returns 当前级别 number 和不是当前级别 model.
所以可以将此方法重命名为 current_level_number
,然后添加一个 current_level
方法,例如:
def current_level
levels[current_level_number - 1] # remember array indexes start at 0
end
那么您应该可以habit.current_level.missed_days
获取当前级别的错过天数。
此外,请注意您的 current_level
方法。在大多数情况下,它 returns 一个数字,即 1 到 5,但在特定情况下 returns 一个字符串 "Mastery"。我想 returns 像这样的不同类型可能会给你带来麻烦。
更多细节:当您养成习惯时,您似乎已经设置了 5 个级别:
def save_with_current_level
self.levels.build
self.levels.build
...
所以这些将是 levels[0]
、levels[1]
到 levels[4]
在您的 DaysMissedController
中,您正在更新习惯本身和其中一个级别的错过天数,所以我假设这是您想要的当前级别的 days_missed
计数抓住。
您的 current_level
方法 returns 基于 n_days
的 1 到 5 之间的级别数字,因此当用户处于我们想要的习惯的级别 1 时 levels[0]
,对于 2 级,levels[1]
依此类推。
这只是一个假设,因为你没有提到很多关联,我什至不确定我是否理解你想要什么,但如果我的想法是正确的,我会 think/assume它会像这样
MissedDay.joins(level: {habit: :user})
.merge(User.where(id: user_id))
.merge(Habbit.where(id: habbit_id))
.count