last day (14 days later) » 

11:55 AM
0
A: How to create and save a predefined checklist collection for users and set up the associations between four models?

maxYou modeling is a bit off: class User < ActiveRecord::Base has_many :checklists, dependent: :destroy has_many :tasks, through: :checklists end class Checklist belongs_to :user has_many :tasks, dependent: :destroy end class Task belongs_to :checklist has_one :user, through: :checkli...

 
Hey @max , thank you very much. So I have to create join table checklists with a task_id and user_id? class CreateAssembliesPartsJoinTable < ActiveRecord::Migration def change create_table :users_tasks, id: false do |t| t.integer :user_id t.integer :taks_id end add_index :users_tasks, :user_id add_index :users_tasks, :task_id end end
Why has the task just one user? Every user should have the same predefined tasks. User has a Dashboard "Cardio" This dashboard contains four checklists with eacht 10 predefined tasks (checkboxes)
 
max
Because if a single task belonged to many users than if one user checks off the task then it becomes checked off for all users...
If you wan't to have predefined tasks you would do it either through something like a Service Object which creates the tasks for a new user.
If you need the presets to be user editedable you need a much more complex setup where the "preset tasks" and "user assigned tasks" are stored on different tables.
 
well that makes more then just sense :) If has_one :user, through: :checklist points to checklists.user_id then I need the foreignkey user_id in checklists?
 
max
Yes
ActiveRecord does not care if you have foreign keys though - but it will make your database enforce referential integrity between the users and checklists tables.
 
12:15 PM
Ok can you explain or point me in a direction according the Service Object?
 
 
2 hours later…
max
1:56 PM
class DefaultTasksService

  def intialize(user)
    @user = user
  end

  def call
    checklist = user.checklists.create
    tasks = [
      {
        title: 'Do something'
        description: "Ipsum loren..."
      },
      {
        title: 'Do something else'
        description: "Ipsum loren..."
      }

    ]

    tasks.map do |t|
      checklist.tasks.create!(t)
    end
  end


  private
    attr_accessor :user
end

# then call it from your controller when the users tasks should be populated
The idea is that it gives you a nice single purpose object which is easy to test and far easier to control than using model callbacks for example.
RSpec.describe DefaultTasksService do
  describe "#call" do
    let(:user) { create(:user) }
    it "creates two tasks" do
      expect {
        DefaultTasksService.new(user).call
      }.to change(user.tasks, :count).by(2)
    end
  end
end
 
 
1 hour later…
3:16 PM
Sorry for my late response. I was looking into after_create and I stuck in some blog :)
wow thank you very much for the code!
So if I understand this right. First it intializes the user model. I think in my case it should be @user = current_user (because I'm using devise)
Because of the fact that the "checklists" already exist, is it possible to add at the create task the checklist id?

e.g
{
title: 'Do something'
description: "Ipsum loren..."
checklist_id: '2'
},
 

last day (14 days later) »