ruby-on-rails - 如何整合:missed days with :committed days in habits. rb?

标签 ruby-on-rails ruby integer rails-activerecord days

我们如何将 t.integer :missedt.text :committed 集成,以便

  1. 当用户在 :level 中检查他 :missed 3 :committed 天时,他必须重新启动 :级别?

  2. 对于他检查的每个 :missed 天,都会将额外的 :committed 天添加回 :level 以便进阶前一定要补上?

“精通”之前,每个习惯都有 5 个级别!

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

我猜我们必须在这里区分 :missed:missed ,这取决于它指的是什么级别。

习惯/_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>

habits_controller.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

_create_habits.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

:committed 完美运行,但现在 :missed 毫无用处。请帮助我添加适当的逻辑以将 :missed:committed 集成。

非常感谢您抽出宝贵时间!

更新

@Dimitry_N 的回答没有达到这个问题的 1) 或 2),就像我试图让它发挥作用一样。也许你会更幸运地融入他的逻辑。通过他的回答,我也得到了这个错误:How to fix level.rb to work with :committed days?

最佳答案

我认为程序设计必须稍微重新评估一下。我相信levelsdays应该是带有类似 level 列的独立模型和 missed (遵循@dgilperez 在他的评论中提到的 SRP 的概念)。因此,我们最终得到四个模型:User , Habit , LevelDay , 具有以下关联:

  • 用户:has_many :habits , has_many :levels
  • 习惯:belongs_to:user , has_many :levelshas_many :days, through: :levels #for being able to access Habit.find(*).days
  • 级别:belongs_to :user , belongs_to :habithas_many :days
  • :belongs_to :level , belongs_to :habit

通过这些关联,您可以创建具有嵌套属性 的表单。有一个 awesome RailCast explaining nested forms .

<%= form_for @habit do |habit| %>
  <% 5.times.each_with_index do |number, index| %> 
    <h1>Level <%= index + 1 %></h1>
    <%= habit.fields_for :levels do |level| %>
      <%= level.fields_for :days do |day| %>
        <%= day.label :missed %>
        <%= day.check_box :missed %> <br/>
      <% end %>
    <% end %>
  <% end %>
  <%= habit.submit "submit" %>
<% end %>

“魔法”发生在 habits_controller 中,看起来像这样:

class HabitsController < ApplicationController
  ...
  def new
    @habit = @user.habits.new
    @level = @habit.levels.new
    3.times { @level.days.build }
  end

  def create
    @habit = @user.habits.new(habit_params)
    @levels = @habit.levels

    if @habit.save
      @habit.evaluate(@user) 
      redirect_to ...
    else
      ...
    end
  end

...
  private

  def habit_params 
    params.require(:habit).permit(
      :user_id,
      levels_attributes:[
      :passed,
      days_attributes:[
      :missed,:level_id]])
  end
...  
end

注意 nested strong params , @habit.evalulate(@user)方法,我将在下面展示,以及 3.times { @level.days.build }调用,它在您的 View 中构建嵌套表单的字段。

habit.evauate(user) 方法: 在新的 Habit 之后调用此方法被保存。评估属性并将错过的天数和级别的 ID 附加到用户的 missed_daysmissed_levels分别属性。逻辑有点笨拙,因为您会将一个数组附加到另​​一个数组,因此您可能会想出更有效的方法。同时:

  def evaluate(user)
    levels.each { |level| level.evaluate }
    user.missed_levels << levels.where(passed: false).ids 
    user.missed_days << days.where(missed: true).ids 
    user.save
  end

请注意对 level.evaluate 的调用,看起来像这样:

  def evaluate
    if days.where(missed: true ).count == 3
      update_attributes(passed: false)
    else
      update_attributes(passed: true)
    end
  end

架构看起来像这样:

  create_table "days", force: true do |t|
    t.integer "level_id"
    t.integer "habit_id"
    t.boolean "missed",   default: false
  end

  create_table "habits", force: true do |t|
    ...
    t.integer "user_id"
    ...
  end

  create_table "levels", force: true do |t|
    t.integer "user_id"
    t.integer "habit_id"
    t.boolean "passed",   default: false
  end

  create_table "users", force: true do |t|
    ...
    t.string   "name"
    t.text     "missed_days" #serialize to Array #serialize to Array in model
    t.text     "missed_levels" #serialize to Array in model
    ...
  end

别忘了使用 accepts_nested_attributes_for :levels, :days对于 Habit 模型,和 accepts_nested_attributes_for :days 用户。 <强> Here is a git with all my code. 让我知道。

关于ruby-on-rails - 如何整合:missed days with :committed days in habits. rb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28799527/

相关文章:

ruby-on-rails - 如何从 url 中删除随机多余的斜杠?

ruby-on-rails - 为什么我的线程变量在 Rails 中是断断续续的?

ruby-on-rails - Mongoid:从 Moped::Errors 获取 mongo 错误代码

mysql - Rails 无效的哈希密码在 ruby​​ 控制台中进行了身份验证,但重新打开后却没有进行身份验证。为什么?

c - 补码截断?

c++ - 创建货币数据类型 C++

ruby-on-rails - 如何在 Rails 3 中运行重点功能测试

ruby-on-rails - 未定义的局部变量或方法 `f'在局部 View 中

事件记录 :condition syntax of an empty field?

php - 如何在 PHP 中将罗马数字转换为整数?