The Problem:
I need to create an application that allows "admin" users to create checklists. Each checklist has :many items. The user will be able to add and remove items from the checklist, as they so choose. (Via a gem like Cocoon, since it handles nested resources so gracefully.)
An example form would look something like this:
Checklist
- Item
- Item
- Item
[Add Items +]
Once a form has been created, normal users would then be able to complete/follow a checklist. Checked is good/true, Unchecked is bad/false.
Automobile
- Check Oil [checkbox]
- Check Tires [checkbox]
- Check Lights [checkbox]
[Submit]
This is the difficult part. One approach I have considered...
Create a Record model, that belongs to the Checklist model,
Checklist
class Checklist < ActiveRecord::Base
has_many :records
end
Record
class Record < ActiveRecord::Base
belongs_to :checklist
end
Then, serialize the results into a hash...
class Record < ActiveRecord::Base
belongs_to :checklist
serialize :list_history, Hash
end
I would use the item.id as the key, and the user true/false input as the value. So, the stored result would look something like:
[ 2 => true, 54 => true, 34 => false]
This way, users could complete a checklist many times, and then an "admin" could pull up the results of previous checklists.
Issue A
This seems like it could work, except for when "admin" users want to edit a checklist. If they remove an "item" from the checklist, even though I can remove the "item" from the association, the "item", must be kept in the database. Otherwise, if Oil has an ID of 2, and Oil is removed in an Edit, then the record's list_history hash will have [ 2 => true], which is a key that points to nowhere. I can visualize a backlog of items pilling up.
Issue B
I'm not sure if this is the best way to tackle this problem. I've been using rails for 2 1/2 years now, and still feel like I did on day 1. (Actually, I I feel as though I know even less.) Maybe there is a better solution just around the corner, that I haven't thought of yet.