我们如何将t.integer :missedt.text :committed集成,以便
当用户在:missed中退出:committed3:level天时,他必须重新启动:level
对于他退房的每一天,都会在退房前再加上一天,这样他必须在退房前补上一天?
每个习惯都有5个等级才能达到:missed

class Habit < ActiveRecord::Base
	belongs_to :user
	before_save :set_level
	acts_as_taggable
	serialize :committed, Array

  def self.comitted_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 levels
			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 }

  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

private
	def set_level
	 self.level = levels
	end
end

我想我们应该根据它所指的级别来区分:committed:level
习惯/u form.html.erb
  <label> Missed: </label>
  <div>
  <label> Level 1: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>
  <div>
  <label> Level 2: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>
  <div>
  <label> Level 3: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>
  <div>
  <label> Level 4: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>
  <div>
  <label> Level 5: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>

习惯控制器.rb
class HabitsController < ApplicationController
  before_action :set_habit, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @habits = Habit.tagged_with(params[:tag])
    else
      @habits = Habit.all.order("date_started DESC")
      @habits = current_user.habits
    end
  end

private

    def habit_params
      params.require(:habit).permit(:missed, :left, :level, :date_started, :trigger, :target, :positive, :negative, :tag_list, :committed => [])
    end
end

_创建习惯.rb
class CreateHabits < ActiveRecord::Migration
  def change
    create_table :habits do |t|
      t.integer :missed
      t.integer :level
      t.text :committed
      t.datetime :date_started
      t.string :trigger
      t.string :target
      t.string :positive
      t.string :negative
      t.references :user, index: true

      t.timestamps null: false
    end
    add_foreign_key :habits, :users
    add_index :habits, [:user_id, :created_at]
  end
end

"Mastery"工作得很好,但现在:missed毫无用处。请帮助我添加适当的逻辑以将:missed:committed集成。
非常感谢你的时间!
更新
@迪米特里安的回答并没有我想的那么成功,也没有我想的那么成功。也许你能更好地理解他的逻辑根据他的回答,我也得到了这个错误:How to fix level.rb to work with :committed days?

最佳答案

你应该把问题做完,因为这是一个问题中的许多要求。Dimitry_n似乎走在了正确的轨道上,但是您现在需要将一些逻辑添加到levels模型中。如果你想了解详情,请和我聊聊。

10-07 22:05