ruby on rails - How to associate "missed_days" with a date? -


a user has many habits. if 1 day misses doing habit suppose click here:

habits/_habit.html.erb

<%= link_to new_habit_level_days_missed_path({ habit_id: habit, level_id: habit.current_habit_level.id }), id: 'remove_check' %>   <span class="glyphicon glyphicon-remove"></span> <% end %> 

enter image description here

he redirected to:

views/days_missed/new.html.erb

<%= simple_form_for(@habit) |f| %>   <%= f.date_field :missed_days_date %>   <%= link_to 'submit'.html_safe, habit_level_days_missed_index_path({ habit_id: @habit, level_id: @habit.current_habit_level.id }), remote: true, method: 'post', data: { modal: true }, class: 'remove-check', id: 'remove_check' %> <% end %> 

where upon clicking submit post request made daysmissedcontroller's create action, increment missed_days +1.

class daysmissedcontroller < applicationcontroller   def new   end    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     if level.missed_days == 3       level.missed_days = 0       level.days_lost += habit.calculate_days_lost + 2     end     level.save!     head :ok # returns empty response 200 success status code   end 

how can associate date_field entry each increment of missed_days? in other words, every missed day there should missed day date.

this way in addition showing how many days user missed show days user missed.

db

  create_table "habits", force: true |t|     t.integer  "missed_days",  default: 0     t.boolean  "conceal",      default: false     t.integer  "likes"     t.datetime "date_started"     t.string   "trigger"     t.string   "action"     t.string   "target"     t.string   "reward"     t.integer  "user_id"     t.datetime "created_at",                                                                      null: false     t.datetime "updated_at",                                                                      null: false     t.integer  "order"     t.datetime "completed_at"     t.text     "committed",    default: "---\n- sun\n- mon\n- tue\n- wed\n- thu\n- fri\n- sat\n"   end    add_index "habits", ["user_id", "created_at"], name: "index_habits_on_user_id_and_created_at", using: :btree   add_index "habits", ["user_id"], name: "index_habits_on_user_id", using: :btree    create_table "levels", force: true |t|     t.integer  "habit_id"     t.integer  "missed_days",   default: 0     t.datetime "missed_days_date"      # how should integrate this?     t.integer  "days_lost",     default: 0     t.integer  "current_level"     t.datetime "created_at",                null: false     t.datetime "updated_at",                null: false   end 

routes

  resources :habits     resources :levels       # route increments , decrements missed days       resources :days_missed, only: [:create, :destroy, :new]     end   end 

here's gist of it.

thanks help!

it depends on want missed_days info. hacky storing missed dates json (or comma separated string!) , storing them in missed_days_date column.

the correct way though, in terms of relational database design, have new missed_dates table, has following fields: id, date_missed, level_id.

then, in rails models, level has_many :missed_dates, , misseddate belongs_to :level.

your daysmissedcontroller become misseddatescontroller , standard crud controller creates new records in missed_dates table. you'll want implement existing missed_days column counter cache column.

this way bit more effort front, , involves table/model, flexible long term. eg, later, may want find out user's reason missing each day (eg. lack of time, lazy, forgot, etc) - , if go hacky solution, hard. if go correct solution, simple adding new reason column missed_dates table.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -