I'm developing an e-learning in Rails and I want to save a set of Arrays to the database, with the aim of tracking a user's progress through the various sections of the e-learning.
I've come across this question and answer:
Storing arrays in database : JSON vs. serialized array
...which sounds like it might be useful, but I can't figure out how to integrate it into the Rails project I'm developing.
Given that I'm pretty much a Rails noob, could someone explain to me, in plain English (or alternatively plain code) how I would:
a) save to a database an Array which holds a sequence of 'false' Booleans when a user initially signs up.
b) retrieve and update that Array from various pages throughout the e-learning.
I would set out what I have tried, but it's just been wild stabs in the dark and I don't really know where to start. Should the Comment class be saved in the controllers folder? Or is it, in my example, actually the User class (which, similarly, extends ActiveRecord)?
Once again, any help (ideally explaining where the code belongs) much appreciated.
UPDATE
I've been asked to be more specific, so I will try:
I have a user table in the database, which (predictably) has several fields, including email, username, etc. I'd like to add 5 more fields to the user table, each storing an Array to track the user's progress through each of the 5 sections. I'm envisaging, when a user opens a piece of content, the Array would be updated and the appropriate index would be updated to 'true' to flag that THAT part of the section is complete.
I hope that helps...
RE-UPDATE
When a user first signs up I want the Arrays to be set with a sequence of 'false' Booleans. From which file would I set the Arrays? Is that done in User? At the moment I'm trying this, but I don't think the values are being set - although it's not throwing an error.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
serialize :leadership_styles_progress
serialize :skills_progress
serialize :safeguarding_progress
serialize :tools_progress
before_create :set_progress_vars
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :age, presence: { :message => ": Please choose your age" }
validates :section, presence: { :message => ": Please choose the section you belong to"}
has_many :posts
private
def set_progress_vars
self.leadership_styles_progress = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
self.skills_progress = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
self.safeguarding_progress = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
self.tools_progress = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
end
end
I have also checked which routes are mapped where, via 'rake routes'; users/sign_up is mapped to devise/registrations/new. So does the serialize code, suggested below, go there?