2

I am having a hard time determining the best methods to store checklists in Rails.

In my application, there are accounts which have many documents. I would like to add a checklist to each document such that the checklist items can be configured by an account. All of the documents will have the same checklist (configured by account) but may have different checked/unchecked values for each item.

The setup would look something like this:

Account
  Document1
    ChecklistForDocument1
      ChecklistItem1 - isCheckedForDocument1
      ChecklistItem2 - isCheckedForDocument1
      ChecklistItem3 - isCheckedForDocument1

  Document2
    ChecklistForDocument2
      ChecklistItem1 - isCheckedForDocument2
      ChecklistItem2 - isCheckedForDocument2
      ChecklistItem3 - isCheckedForDocument2

Just wondering how I can setup the models/migrations to store these types of checklists.

I have looked at several other questions such as Database Design for Storing Checklists and Results and What would be a good approach for storing the results of a checklist in Rails? but each of them differ slightly from my situation and it's really throwing me off.

Community
  • 1
  • 1
Jack C
  • 1,044
  • 2
  • 12
  • 22

1 Answers1

2

Do this, create a join table that contains a document_id and check_list_item_id

DocumentCheckListItem
  belongs_to :document
  belongs_to :check_list_item

Then change your existing models

Document
  has_many :document_check_list_items
  has_many :check_list_items, :through => :document_check_list_items


CheckListItem
  has_many :document_check_list_items
  has_many :documents, :through => :document_check_list_items

Then, all you need to do is refer to document.check_list_items in your code.

Setting check_list_items on a document is done by sending an array of id's to this method

document.check_list_item_ids = [1,2,3,4,5]

That will set the values and wire up the relationships.

Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70
  • So the DocumentCheckListItem relationship will have the is_checked property and the CheckListItem will have the item_name property? How can I specify that the account sets which CheckListItems are available to the documents owned by the account? – Jack C Nov 04 '16 at 23:52
  • 1
    The relationship's existence represents "is_checked". The relationship would be destroyed if it was unchecked in the form. A name column would be on the CheckListItem object and be reused. – Mark Swardstrom Nov 04 '16 at 23:55
  • Okay, that makes sense, thanks. And how would I control which CheckListItems are available through the account model? – Jack C Nov 05 '16 at 00:00
  • It sounds like `check_list_item` will have an `account_id` on it (and `belongs_to`). You'll just create them normally and show them all to generate the checkboxes when you edit/create the document. A note, the checkboxes in a form are another challenge, but that's another question, you'll end up doing some research on that. – Mark Swardstrom Nov 05 '16 at 00:02
  • I have everything working now (thank you), except I cannot set the relations. I am doing document.check_list_item_ids = ["33","34","36"] as well as document.save – Jack C Nov 07 '16 at 00:36
  • The request does not fail but it also does not update the document.check_list_item_ids and the view redirects to the #show view however some items from the #edit view are still visible on screen for some reason. – Jack C Nov 07 '16 at 00:39
  • Actually after further debugging, I can't seem to access `document.check_list_items` or `document.check_list_item_ids` at all – Jack C Nov 07 '16 at 01:08
  • 1
    Questions about this may be a new question on SO. You have to make sure it's an allowed parameter in the controller, there are other issues that could potentially break this. Try it in console to see that you can access those methods. Check out - http://guides.rubyonrails.org/association_basics.html#methods-added-by-has-many – Mark Swardstrom Nov 07 '16 at 18:11