3

Say I have a Student model and a Class model, and

Student.belongsToMany(Class, through: {StudentClasses}).

In the DB, StudenClasses table has id, student_id, class_id, and id like to have all the associated student_ids of a Class instance by that table

How can I have on my Class model an array containing all of the associated Student IDs?

I've been struggling for a few days now, any help would be much appreciated!

Kesem David
  • 2,135
  • 3
  • 27
  • 46
  • I only found a work around solution. Get *Class* by *id*. Do not forget to include all attributes. So we will have an array of Student here. And then get all ids – Toan Tran Aug 30 '17 at 06:48
  • Ahhh Why dont you query on **StudentClass** model? – Toan Tran Aug 30 '17 at 07:10
  • @ToanTran Thanks for your replies! :) What I'm trying to achieve is having this array on an instance, without having to query other models for that information.. unfortunately i ended up using the `getClasses().map(class => class.classId)` ... :\ – Kesem David Sep 01 '17 at 21:11

0 Answers0