Return a record by it's name not Id


#1

Hi. I have this code that return a timeSlot record in a table and filter by from and zoneId and sorted by from. On, template.hbs there is dropdown selection that would display a certain record by it’s zoneID It is working fine and i have no problem with that.

However, is there any approach where i could return a record by it’s zone name? The purpose of returning a record by it’s name is because I would like to create a table that has a record of timeslot by it’s name in a different column.

My goal right now is to return something like that. When I selecte “All Zones” in a dropdown.

Here’s my code

model(params) {
    return this.store.query('zone', { sort: 'name' })
      .then((allZones) => {
        const zoneId = params.zoneId || allZones.get('firstObject.id');

        return Ember.RSVP.hash({
          zones:     allZones,
          zone:      this.store.findRecord('zone', zoneId),
          timeSlots: this.store.query('timeSlot', {
            'filter[from]':    params.from || moment().format('YYYY-MM-DD'),
            'filter[zone-id]': zoneId,
            'sort':            'from'
          }),
       });
    });
}

#2

I’m not completely sure if I understand what you are trying to accomplish, but what about using

this.store.queryRecord('zone', { name: zoneName })

The queryRecord function will return just one record and use query parameters to fetch it. Your HTTP request URL would look something like /zones?name=THE-ZONE-NAME.