Topic: Feedback on my model structure and One-Way has_many :through

Hi everyone,

I've asked similar questions before here, but I've been learning a ton since I last asked about this and have revised my app's structure considerably.

Anyways, I have an app for taking classes online. Currently, here is my structure (the relevant parts anyways):

User
  has_many :subscriptions
  has_many :courses, :through => :subscriptions
Course
  has_many :subscriptions
  has_many :users, :through => :subscriptions

I'm working on adding the Exam and Grading parts of the app and am currently working with this for my model associations:

User
  # all of above
  has_many :grades, :through => :subscriptions
Course
  # all of above
  has_many :lessons
Grade
  belongs_to :lesson
  belongs_to :user # this is the part that gets kind of hairy; should this belong to :subscriptions or :users
Lesson
  has_one :grade
  belongs_to :course # this also seems weird, but it's necessary that a lesson is related to Course for sure
  has_many :questions
Question
  has_many :answers
  has_one :correct_answer # a pseudo-model based on answers with the correct boolean set to 'true'
  belongs_to :lesson
Answer
  belongs_to :question

What do you guys think? Especially about the Grade model. How's the proper way to express a one-way has_many :through association?

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com

Re: Feedback on my model structure and One-Way has_many :through

This could be really really complex. God, I have a headache just thinking about it so I'll keep it as simple as I can.

Let's see if I have this right (Always start with pen and paper before you start modelling databases.) as I think you have a missing concept but not sure yet!
A course consists of lessons which in turn have questions and questions have answers
A student subscribes to a course and attends a lesson?
Therefore a lesson has many attendees
each attendee needs grading on their answers for a lesson, therefore a lesson has many grades and a student has a grade for each lesson
In order to grade an attendee (A subscribed student) you need to know what answers an attendee has provided and I don;t see that anywhere in your models)
I'm going to stop there just in case I have any of that wrong. Come back to me with your ideas/thoughts

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Feedback on my model structure and One-Way has_many :through

Thanks for replying! I know what you mean, this is complex, but I don't know of another way to go about it. I've had a tough time trying to figure out how to do this, but I think I got it.

User
  has_many :subscriptions
  has_many :courses, :through => :subscriptions
Course
  has_many :subscriptions
  has_many :users, :through => :subscriptions
  has_many :lessons

Subscription # this model has a state-machine that tracks the state of the user's subscription (failed, passed, pending, etc...)
  belongs_to :course, :user
  has_many :grades

Lesson #each lesson has an exam to go along with it. I'm doing it this way to allow flexibility in the future for adding different types of exams
  belongs_to :course
  has_one :exam
Exam # the controller for this model handles the making of exams
  belongs_to :lesson
  has_one :grade
  has_many :questions
  accepts_nested_attributes_for :questions
Grade #The controller for this model will handle the actual taking of an exam
  # This also has a field for saving the answers a student makes(multiple choice questions) as a comma delimited string 
  belongs_to :subscription, :exam

Question
  belongs_to :exam
  has_many :answers
  accepts_nested_attributes_for :answers
Answer
  belongs_to :question

It's starting to get complicated, but at least it makes sense now. The routes are kind of crazy too. I've got to look into that, because to avoid deeply nested routes, I'm having to write multiple `resources :resource do` blocks and currently have a bunch of unnecessary routes.

Do you guys see any blaring mistakes/oversights in the structure shown above?

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com

Re: Feedback on my model structure and One-Way has_many :through

That's a lot better
A user takes many exams?
Therefore you grade an exam for a user.
So a user has_many exams through subscriptions
a user has many grades through exams - I think Rails 3.x + will allow this deeply nested relationship

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)