← Back to Upcase

Schema advice for a school/course app


(Charlie Gaines) #1

I am building a site where users can take various sequences of courses and I am struggling to figure out the schema. So far I have this:

class User
  has_many :active_courses
  has_many :assigned_lessons, through: :active_courses
end
class Course
  has_many :lessons
  has_many :active_courses
end
class ActiveCourse
  has_many :assigned_lessons
  belongs_to :user
  belongs_to :course
class AssignedLesson
  belongs_to :active_course
  belongs_to :lesson
end
class Lesson
  has_many :assigned_lessons
end

I fear this is over-complicated. Is there a more simple way to structure an application of this sort? I need the lessons in each course to have a particular order - which can be chosen by the admin when they create a course. And I need to be able to mark active lessons and assigned courses as completed. Any advice much appreciated!


(Ben Orenstein) #2

Doesn’t look obviously over-complicated to me.

I might call ActiveCourse Enrollment instead, but that’s a personal call.

I’d say start building and see how this works out for you.


(Charlie Gaines) #3

Thanks for your response Ben. Something I am struggling with is how to handle the ordering of lessons in the course. I can’t have an order attribute in my Lesson class because lessons might be in more than one course. Any advice?


(Pedro Moreira) #4

@charliegaines, maybe a Syllabus model that holds Lesson order in relation to Course or, in case, an entity responsible for that order?


(Ben Orenstein) #5

Yeah, I’d likely do what @pedromoreira is suggesting.


(Charlie Gaines) #6

Thanks fellas. So…a Course would have one Syllabus, and Syllabus has many Lessons. How would I store the order in Syllabus?

The direction I was going in was to have a CourseLesson join model with an :order attribute on that model. I haven’t managed to figure out how to set the order in my Course#new form though. I tried this:

      <div>
        <%= check_box_tag "course[lesson_ids][]", lesson.id %>
        <%= lesson.title %>
      </div>
      <div>
        <p>Order of lesson in course:</p>
        <%#= number_field_tag "course[lesson_order]", in: 1...8 %>
      </div>

But no luck. My terminal output:

"course"=>
  {"title"=>"Foobar",
   "description"=>"Feh",
   "category"=>"Basics",
   "lesson_ids"=>["1", "2", "3"],
   "lesson_order"=>["2", "1", "3"]}

Any tips? Thanks again for your help.


(Charlie Gaines) #7

@pedromoreira, if I take the Syllabus approach, how would you store the order? It seems the simplest approach is to store the meditations ids in a postgres array. But I could just do that in my Course class and dispense with Syllabus, no?


(Pedro Moreira) #8

@charliegaines, domain modelling questions are always tricky.

Your CourseLesson approach is the same as Syllabus: the difference being the naming and clarity it adds or not to your domain model. From the information you presented, I would store the order as an attribute in a join model, just as you suggested, since I don’t know enough about your application to say otherwise.

You mentioned you were having problems with that approach in that you aren’t sure how to handle the new Course form. It appears you are making a form that creates the Course and adds in the lessons in order. Maybe you can expose more of your problem with that? It is something that is easier to get help with :slight_smile:


(Charlie Gaines) #9

@pedromoreira, yes they are tricky - but also interesting! Thanks for your response, I will keep hacking away.

@benorenstein, would you consider doing some weekly iterations or a series of screencasts on the topic? Maybe do it Iron Chef Developer style, have two developers tackle a tricky domain and compare results. I think that would be interesting.


(Pedro Moreira) #10

@charliegaines, you’re welcome. Hope I helped a bit :slight_smile: