I'm building a math exercise app. I want to store questions that are separated into exercises and each exercise belongs to a different section. Also there are multiple sections for each grade (12 grades in total).
How do i track a user's answer (action)? For instance, when a user browses the app, decides to answer question 7 from exercise 'G1' in section 'G' of Grade 'Pre-K' and provides a correct answer i want to track that and in the future provide him with statistics about his performance.
Currently there isn't a connection between my User model to the questions and i wanted to hear from others as to what would be the most efficient way to tackle this (maybe the models right now are completely wrong)
This is the model diagram for now with has_many, belongs_to relationship (i'll add 'the_question':string and 'answer':string later on to the question model):
I think all that is missing is a Answer model, that is connected to the User.
Optionally, you can define the relations on the "parents" of Question too.
class Grade < ActiveRecord::Base
has_many :users, :through => :sections # optional. i.e. 'users that have answers in this grade'
end
class Section < ActiveRecord::Base
has_many :users, :through => :exercises # optional
end
class Exercise < ActiveRecord::Base
has_many :users, :through => :questions # optional
end
class Question < ActiveRecord::Base
has_many :answers
has_many :users, :through => :answers # optional
end
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
end
class User < ActiveRecord::Base
has_many :answers
end
Update: re-reading your description, I think CorrectAnswer would be better suitable than Answer.
Related
I would like to achieve something as follows where PersonSubject has many topics, but the choices of these topics are limited to the the selection of topics through another model (ie: through the associated subject):
class Topic < ApplicationRecord
belongs_to :subject
end
class Subject < ApplicationRecord
has_many :topics
end
class PersonSubject < ApplicationRecord
belongs_to :person
belongs_to :subject
has_many :topics # where the choices are limited to the subject.skills
end
I would then like if any person_subject.subject.topics are deleted (or association removed), it would automatically update the person_subject.topics to no longer "point" to the Topic(s) that were deleted.
Is this possible?
You can use a lambda to put arbitrary filters on an association. See What is the equivalent of the has_many 'conditions' option in Rails 4?
has_many :topics, -> { where(skill: subject.skills) }
I don't know that this is exact code will work without seeing your schema (what is the data type of subject.skills, and how do you join this with topic?). But hopefully this gets you on the right track
edit
in response to your comment, I think
has_many :topics, through: :skills
would work
I want to store complex form/questionnaire data but I'm having a hard time designing the database:
A User can create a new Exam. When creating an Exam, the user needs to choose from a template with 30 pre-set questions (and I want to store the answer to each of these questions in the database).
Purpose of the template is so that practitioners might want to run different exams (e.g. knee exam, arm exam, spine exam etc). So they create a new exam, choose a specific template. And each template has many questions
What's the best way to design this database? I started creating Exam, Template, and Question models, but then I got lost building the relationships.
Here's how I ended up designing the database. I added a 4th "answers" model. This way, I can associate answers to questions, and questions to templates, and templates to exams.
Hope this helps someone
class User < ActiveRecord::Base
has_many :patients
has_many :assessments
end
class Patient < ActiveRecord::Base
belongs_to :user
has_many :assessments
end
class Assessment < ActiveRecord::Base
belongs_to :user
belongs_to :template
belongs_to :patient
has_many :questions, :through=> :template
has_many :answers, :through=> :questions
end
class Template < ActiveRecord::Base
belongs_to :user
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :template
has_one :answer
end
class Answer < ActiveRecord::Base
belongs_to :question
end
I'm getting crazy adding some features to my quiz app based on Rails and Active Record.
This is my actual data model:
class Quiz < ActiveRecord::Base
has_many :quiz_questions
has_many :questions, through: :quiz_questions
belongs_to :user
end
class Question < ActiveRecord::Base
belongs_to :section
has_many :answers
has_many :quiz_questions
has_many :quizzes, through: :quiz_questions
end
class Answer < ActiveRecord::Base
belongs_to :question
end
class Section < ActiveRecord::Base
has_many :questions
end
class User < ActiveRecord::Base
has_many :quizzes
end
Question and answer are obvious: I store the questions and every question has some answer.
The logic now is working right.
User create a quiz, the system take some random questions and add them to the join table.
I must add now some stats: I want to add the possibility for every user to track the right answer percentage and the questions with more errors and I want also to give to every user the possibility to choose a "quiz generation strategy", overriding the "randomness" of the question choice. User for example can create a quiz with questions he never answered (there are about 1200 question in the table and every quiz has 30 questions), or user can create a quiz with questions he wrongs often.
I cannot find a way to add these counters in a smart way. I have my join table, but using the join table I have poor performance. Any idea?
I'm creating a 'work book' ruby on rails application. Basically a user will read a paragraph or two and then answer some questions based on what they've read.
I'm unsure how to model this. The paper work books have many pages and each page has many paragraphs.
Should I model it exactly as it is in physical form or should I simplify the model?
My biggest question is how do I save state so that a user can update their workbook and or make corrections after they've submitted part of their assignment?
I think these relations could work for you:
class WorkBook
has_many :pages
class Page
belongs_to :work_book
has_many :paragraphs
class Paragraph
belongs_to :page
has_many :questions
class Question
belongs_to :paragraph
has_many :answers
class Answer
belongs_to :question
belongs_to :user
class User
has_many :answers
has_many :answered_questions, class_name: 'Question', through: :answers
If you want a better control on your answers (like value_string, value_float, value_boolean, etc.) I recommend you to add an extra model "AnswerValue".
You might feel like there is too much models in this case, but trust me, it makes your app flexible and allows you to add more "granular" features on each model / relation.
An extenstion with these relations: You could add a model "AnswerCorrection" which would act like an Answer but would be a way to save another value after the user update it's workbook.
Hope this helps!
I'd go for something like this:
class PaperWork < ActiveRecord::Base
has_many :pages
has_many :paragraphs, through: :pages
end
class Page < ActiveRecord::Base
belongs_to :paper_work
has_many :paragraphs
end
class Paragraph
belongs_to :page
belongs_to :paper_work, through: :page
end
Then you can model assignments somewhat around this idea:
class Asignment < ActiveRecord::Base
belongs_to :user
belongs_to :paragraph
# attributes: description, status(pending, completed, etc.)
end
I’m creating a voting feature for our website in the style of YouTube “Likes” and “Dislikes” and Digg using Ruby on Rails 3. I have having trouble coming up with the right scheme.
I have three models, Users, Topics, and Votes. Each User will make one vote “Like” or “Dislike” for one Topic. Like these sites, Users can Vote on a Topic, but they can also create new Topics. I’d like to be able to not only view all of a User’s Votes, but also both the Topics they have created and the Topics they have voted on. I am trying to build this on our own and decide how best to setup the database to handle this process.
My first idea was to use :has_many and belongs_to exclusively like so…
class User < ActiveRecord::Base
has_many :votes
has_many :topics
class Topic < ActiveRecord::Base
has_many :votes
belongs_to :users
class Vote < ActiveRecord::Base
belongs_to :topics
belongs_to :users
boolean choice #tracks whether the User chooses Like or Dislike
But it became evident that this might not be the best way to do this. I began to think the best method would be to use a :has_many :through association like...
class User < ActiveRecord::Base
has_many :votes, :through => :topics
But I’m not sure. Any ideas on how best to set something like this up?
I think your initial setup is good but it can be further improved upon to better support what you want to accomplish. Perhaps like this:
class User < ActiveRecord::Base
has_many :votes
has_many :topics
#Lists all topics the user has voted on
has_many :voted_topics, :through => :votes, :source => :topic
#Lists all votes for the users topics
has_many :topic_votes, :through => :topics, :source => :votes
end
class Topic < ActiveRecord::Base
has_many :votes
belongs_to :user
end
class Vote < ActiveRecord::Base
belongs_to :topic
belongs_to :user
end