jeudi 19 février 2015

Ember.js Modelling Database with HABTM Join Table

I've built a Rails backend for my site and it's database is a little confusing. I'm having trouble modelling it in Ember.js.



  • Each schedule has and belongs to many courses through registrations

  • Each course has many sections


This is where it gets really confusing:



  • Each schedule has many sections, where there exists a record in registrations accordingly


Basically, I can't just call @schedule.courses.collect {|c| c.sections.all } because that will return all the sections for each course. Instead, I just want the sections like so: Registration.all.where(schedule_id: params[:id])


I use a custom serializer in my backend to render it nicely.


So, there's a Course table, Section table, Schedule table, and Registration table. Here's an abbreviated summary of the models. Registration uses a composite key of the three values.



class Course < ActiveRecord::Base
has_many :sections, dependent: :destroy

has_and_belongs_to_many :schedules, join_table: 'registrations'
end


class Schedule < ActiveRecord::Base
has_and_belongs_to_many :courses, join_table: 'registrations'
has_many :sections, through: :courses
end


class Registration < ActiveRecord::Base
self.primary_keys = :schedule_id, :course_id, :section_id

belongs_to :schedule
belongs_to :course
belongs_to :section
end

class Section < ActiveRecord::Base
belongs_to :course # For the course
end


So, my question is how would I model this confusing relationship in Ember.js? I know I'm not supposed to copy the exact structure but I don't know if there's really another way to do it.





Aucun commentaire:

Enregistrer un commentaire