0

I am attempting to build a learning application that has a quiz on it. The quiz is made up of several steps.

I am using aasm to track the the user's state on the quiz. There are 3 states:

1.not started (Default state)

2.in progress 

3.completed

I am tracking the state because when a user starts the quiz, logs out and then returns they will be able to continue.

There are 2 event transitions: start & finish

I understand that I call these on quiz to set the state - Quiz.start! & Quiz.finish!

I can tell what state the user is on if the quiz step they're on has a parent then they're in progress and if the quiz step they're on has no children then they have finished. Something like this:

if quiz.parent
  quiz.start!
else quiz.children == 0
  quiz.finish!
end

All I want to know is where I put the above logic to set the state? I don't think I put it in my controller. Do I have to put it in the Quiz class? Do I use a define a method set_state with the above logic in it? If so, how do I use it?

I have read the aasm docs and kind of get it but my brain is fried and could just use a bit of guidance.

Thanks in advance.

Afsanefda
  • 3,069
  • 6
  • 36
  • 76
Koko
  • 15
  • 3
  • you can put this in the controller, or define a new method on the model, it's up to preference. can you show the controller code where you are thinking of putting it? – max pleaner Jun 15 '18 at 16:09
  • Thanks for coming back so quickly. It is just in my quiz#show action which is currently empty. If I define a method in the model then how do I use it? Can I call it in the show action? Sorry I am sure this is such a basic question but I cannot think! – Koko Jun 15 '18 at 17:20
  • if you define a method on the model you call it on an instance of the model. yes you can do it from the show action. – max pleaner Jun 15 '18 at 18:25
  • I guess it's better to put this kinds of logic in your model so that you can access it easier in everywhere ! @Koko – Afsanefda Jun 18 '18 at 11:18

0 Answers0