nicklandgrebe / active-resource.js

ActiveResource.js - API resource relational mapping in JavaScript
https://active-resource.js.org
MIT License
133 stars 20 forks source link

Association URLs #42

Open steveclarke opened 5 years ago

steveclarke commented 5 years ago

Thanks for creating this library!

I'm getting unexpected results from a query on an association. When I do a find on my Event class:

Event.find('1')

I get the expected result, and the API query URL is http://localhost:5000/events/1/

When I query for the related 'bookings' on an event like so:

let event = await Event.find('1')
let bookings = await event.bookings().load()

My bookings contains a collection of bookings, but it contains ALL bookings, not just ones for the associated event. When I look at the Chrome 'Network' tab I see that the URL for the bookings request is http://localhost:5000/bookings/.

I would have expected this query to be http://localhost:5000/events/1/bookings/.

Is there a configuration parameter I'm missing? Or is my API routing not configured in a manner that conforms to the standard that the library is expecting? I'm using Ruby on Rails for my back-end if that helps any.

Event.js

import MyResources from './MyResources'

class Event extends MyResources.Base {
  static define () {
    this.className = 'Event'
    this.attributes(
      'firstName',
      'lastName',
      'streetAddress',
      'city',
      'province',
      'country',
      'email',
      'phone'
    )
    this.hasMany('bookings')
  }
}

export default MyResources.createResource(Event)

Booking.js

import MyResources from './MyResources'

class Booking extends MyResources.Base {
  static define () {
    this.className = 'Booking'
    this.attributes('id', 'eventId', 'customerId')
    this.belongsTo('event')
  }
}

export default MyResources.createResource(Booking)
nicklandgrebe commented 5 years ago

What is the full JSON response from the server when you request Event.find(1)