Szymon Borucki
Szymon Borucki

Reputation: 407

Fetching records and nested relations

I have a route in my app (repo) where i fetch bookings belonging to a user, I also include rentals and rentals.rental-ratings in the request. Now, a rental (which belongsTo a Booking) hasMany rentalRatings, and rentalRatings belongsTo Users. In the template I have a component that accepts booking and a rentalRating as arguements. How can I get a rating that belongsTo the booking and the currentUser?

Bookings route:

import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {
  sessionAccount: Ember.inject.service(),

  model() {
    const currentUser = this.get('sessionAccount.currentUser');

    return this.get('store').query('booking', {
      filter: {
        'user_id': currentUser.id
      },
      include: 'rental,rental.rental-ratings'
    });
  }
});

Bookings template:

<h1>Your bookings</h1>
{{#each model as |booking|}}
  {{booking-card booking=booking rating="??? what goes here???"}}
{{/each}}

Upvotes: 0

Views: 32

Answers (1)

Jacob van Lingen
Jacob van Lingen

Reputation: 9537

Easiest way to achieve this, is to use the ember-composable-helpers.

Move the sessionAccount service into the controller. In your template you can just use the filter-by helper:

{{#each model as |booking|}}
  {{booking-card booking=booking rating=(get (filter-by "user" sessionAccount.user booking.rental.rentalRatings) "firstObject")}}
{{/each}}

Upvotes: 1

Related Questions