My EmberJS application is connected to a Rails 4 REST API. While the application is generally working fine, it has started to slow down due to the nature of the queries being made.
Currently, the API response looks like this:
"projects": [{
"id": 1,
"builds": [1, 2, 3, 4]
}]
The issue arises when a user has numerous projects with multiple builds within them. EmberJS currently fetches the builds
key and then sends a request to /builds?ids[]=1&ids[]=2
, which is the behavior I want.
This dilemma can be solved in one of two ways.
- Enhance Rails to load the build_ids more efficiently
- Upgrade EmberJS to accommodate different queries for builds
Option 1: Enhance Rails
I have attempted various solutions related to eager loading and manually extracting IDs using custom methods on the serializer. However, both of these methods involve adding excessive code that I prefer to avoid and still result in individual queries per project.
By default, Rails also performs SELECT *
style queries when dealing with has_many associations, and I am unsure how to override this at the serializer level. I also tried a messy solution that consolidated everything into a single fast query by using raw SQL, but I understand that such an approach goes against the Rails convention and would lead to a complex untestable query as the default scope, which is not ideal.
Option 2: Modify EmberJS Queries
Instead of fetching /builds?ids[]=1&ids[]=2
, I prefer not to include the builds key in the project object altogether and make a request to /builds?project_id=1
when accessed in Ember. I believe I can achieve this manually on a field-by-field basis by utilizing something similar to the following:
builds: function () {
return this.store.find('builds', { project_id: this.get('id') });
}.property()
as opposed to the current:
builds: DS.hasMany('build', { async: true })
It is also important to note that this issue extends beyond just "builds". There are four other keys in the project object that exhibit the same behavior, resulting in four queries per project.