Not filterBy properly;


#1

First of all, every class has many subjects and every class has many students. Every subject and student belongs to one class. Also, every subject and student has many grades. But one grade belongs to one subject and one student.

So, before I insert mark (property of the model grade) for a student from a certain subject. It correctly insert a mark for it, but there are two bugs:

  1. It always creates a new object of a grade for the first time when I enter that option, even though it is an existing subject and student with their id’s and also at the same time writes an error “Error: no record was found at …”

  2. Once I insert a mark from that subject and try to insert a mark from another subject it always keep inserting marks from the first subject (previous one);

-I will leave here my snippets of a code from models, template, controller and sample of data from firebase:

app/models/class.js:

import DS from 'ember-data';

export default DS.Model.extend({
	name: DS.attr('string'),
	subjects: DS.hasMany('subject'),
	students: DS.hasMany('student')
});

app/models/grade.js:

import DS from 'ember-data';

export default DS.Model.extend({
	marks: DS.attr('array'),
	subject: DS.belongsTo('subject'),
	student: DS.belongsTo('student')
});

app/models/subject.js:

import DS from 'ember-data';

export default DS.Model.extend({
	title: DS.attr('string'),
	class: DS.belongsTo('class'),
	students: DS.hasMany('student'),
	grades: DS.hasMany('grades')
});

app/models/student.js:

import DS from 'ember-data';

export default DS.Model.extend({
	firstName: DS.attr('string'),
	lastName: DS.attr('string'),
	class: DS.belongsTo('class'),
	grades: DS.hasMany('grade')
});

app/controller/overviewofstudent.js:

import Ember from 'ember';

export default Ember.Controller.extend({
  showS: false,
  mark: "",

  actions:{
    showSubjects(){
      this.toggleProperty('showS');
    },
     
  insertMark(val, val2){
		var student = this.get('store').peekRecord('student', val);
		var subject = this.get('store').peekRecord('subject', val2);
		var mark  = this.get('mark');

		var grades = this.get('store').peekAll('grade');
		grades.filterBy('student', student).filterBy('subject', subject);

		if(grades.get('firstObject') == null){
			this.get('store').createRecord('grade', {
				marks: [mark],
				student: student,
				subject: subject
			}).save().then(function(grade){
				student.get('grades').pushObject(grade);
				student.save();
				subject.get('grades').pushObject(grade);
				subject.save();
			})
		}else{
			grades.get('firstObject').get('marks').pushObject(mark);
			grades.save();
		}
	}
});

app/templates/overviewofstudents.hbs:

<br> <br> <br> <br> <br>
<center>

<table>

<h2> Chosen class: {{model.name}} </h2>

<br>

<i> Current students in class: </i> <br>

<ol> 
{{#each model.students as |student|}}
  <li> {{student.firstName}}  {{student.lastName}} </li>
  <button class="choose" {{action 'showSubjects'}}> Show subjects </button>
  {{#if showS}}
    <br> Current subjects in class: 
    {{#each model.subjects as |subject|}}
      <br> <b> {{subject.title}} </b>
       Choose grade: 
      <select onchange={{action (mut mark) value="target.value"}}>
        <option value="1"> 1 </option>
        <option value="2"> 2 </option>
        <option value="3"> 3 </option>
        <option value="4"> 4 </option>
        <option value="5"> 5 </option>
      </select>
      <br>
      <button {{action 'insertMark' student.id subject.id}}> Insert mark for student </button>
    {{/each}}    
  {{/if}}
{{else}}
  <b> Currently there are no students for the class! </b>
{{/each}}
</ol>

</table>
</center>

-Sample of data from firebase:


Is there a way to check if some record exists before I create a new one?