0

I'm building my first rails/ember app, researching with various sources from tutorials and ember documentation. However, I've hit a wall and after alot of digging, I'm still unable to find the answer I'm looking for. Currently, I'm building a GiftList Rails/Ember app where the index page is the Relationships, and a relationship has many recipients. Right now, I have the Relationships displaying and I want it to display the number of recipients of each relationship however each relationship displays 0 when in fact, at least one of the relationships has one recipient(Family has one record). When I check the data in the Ember inspector, GiftList.Recipient has 1 record and GiftList.Relationship has 4 records. How do I show the correct number of recipient records correctly?

ROUTES

Router.js.coffee:

GiftList.Router.map ->
  @resource 'relationships', { path: '/' }, ->
    @resource 'relationship', { path: ':relationship_id' }, ->
      @resource 'recipients', { path: 'recipients' }, ->
        @resource 'recipient', { path: ':recipient_id'}

relationships_route.js.coffee:

GiftList.RelationshipsRoute = Ember.Route.extend
  model: ->
    @store.find('relationship')

recipients_route.js.coffee:

GiftList.RecipientsRoute = Ember.Route.extend
  model: ->
    @store.find('recipient')

CONTROLLERS

relationships_controller.js.coffee:

GiftList.RelationshipsController = Ember.ArrayController.extend({})

relationship_controller.js.coffee:

GiftList.RelationshipController = Ember.ArrayController.extend
  recipients: GiftList.Recipient.find()

MODELS

relationship.js.coffee:

GiftList.Relationship = DS.Model.extend
  name: DS.attr('string')
  recipients: DS.hasMany('recipient')

recipient.js.coffee:

GiftList.Recipient = DS.Model.extend
  first_name: DS.attr('string')
  last_name: DS.attr('string')
  relationship: DS.belongsTo('relationship')

TEMPLATES

relationships.handlebars:

<h2>Relationships</h2>
  <ul id="relationships">
    {{#each}}
      <li>{{name}}({{recipients.length}})</li>
    {{/each}}
  </ul>

*UPDATE***

SERIALIZERS

class RecipientSerializer < ActiveModel::Serializer
  embed :id
  attributes :id, :first_name, :last_name
  has_one :relationship, key: :relationship

end

class RelationshipSerializer < ActiveModel::Serializer
  embed :ids, include: true
  attributes :id, :name
  has_many :recipients, key: :recipients

end

All other code stayed the same

user2668042
  • 103
  • 1
  • 2
  • 6

1 Answers1

0

I will assume you are using an ember-data beta build. If you are using an older version, this answer will not apply.

You are fetching all relationships and all recipients when what you want to fetch is the recipients for a particular relationship. If you don't want to embed the hasMany relation in the parent model as sideloaded records, then you need to declare the relationship asynchronous:

GiftList.Relationship = DS.Model.extend
  name: DS.attr('string')
  recipients: DS.hasMany('recipient', { async: true })

Then you just need to get the recipients for the relationship:

GiftList.RecipientsRoute = Ember.Route.extend
  model: ->
    @modelFor('relationship').get('recipients')

This will make an async call to /relationships/:id/recipients to get the recipients for the relationship, because you asking for a asynchronous hasMany property that is not yet loaded on the relationships model.

buuda
  • 1,427
  • 7
  • 8
  • thanks for responding. Yes I am using embder data beta: 1.0.0 beta. I've tried your suggestions but it didn't work. I should mention that I'm getting an Uncaught Type error in my console that's caused by recipients: GiftList.Recipient.find() in my relationship_controller.js.coffee file. Do you think that's what's causing it to not work? – user2668042 Feb 28 '14 at 20:55
  • Try removing this line from your controller: `recipients: GiftList.Recipient.find()` . I am going from memory so syntax may be slightly off, but google the async keyword or search the ember-data source for documentation. – buuda Feb 28 '14 at 21:23
  • Actually, the syntax is correct. I tracked down what the issue was and the problem was my model wasn't serialized correctly. I'll post the update tomorrow. Thanks for your help. – user2668042 Mar 01 '14 at 07:51