With a cursory glance, it looks like the STI attribute is hard-coded to 'type', though the with the problem I am seeing personally, it could be that or a polymorphic association (unlikely, but maybe both). Either way, the issue can be reproduced with the setup below.
Also, may not be the best idea to assume the model has an :id field, instead you should be reading type from the parent's inheritance_column attribute and its primary_key from it's primary_key attribute.
If I get a chance today, I will look into fixing this, but thought I would report it in case I am unable to get to it before I forget about it lol
Create a new rails app:
$ rvm use 2.1.2
$ rails --version
=> Rails 4.1.14
$ rails new testtest
$ cd testtest/
$ rails g model ParentModel i_got_an_sti_once:string poly_rel_uid:string poly_rel_type:string
$ rails g model HasManyChildModel uid:string
$ rake db:migrate
Add jsonapi-resources
# Gemfile
...
gem 'jsonapi-resources'
...
Add your routes
#./config/routes.rb
Rails.application.routes.draw do
jsonapi_resources :has_many_child_models, param: :uid
end
Add your controller
# ./app/controllers/has_many_child_controller.rb
class HasManyChildModelsController < ApplicationController
include JSONAPI::ActsAsResourceController
end
Add your parent model
# ./app/models/parent_model.rb
class ParentModel < ActiveRecord::Base
self.inheritance_column = 'i_got_an_sti_once' # That's not true :'(
end
Add your child model
# ./app/models/child_model.rb
class ChildModel < ParentModel
belongs_to :poly_rel, polymorphic: true
end
Add your model that has_many child models
# ./app/models/has_many_child_model.rb
class HasManyChildModel < ActiveRecord::Base
# attributes: :uid
has_many :child_models, as: :poly_rel, primary_key: :uid, foreign_key: :poly_rel_uid
end
Add your has_many_child_models resource
# ./app/resources/has_many_child_model_resource.rb
class HasManyChildModelResource < JSONAPI::Resource
has_many :child_models, polymorphic: true, foreign_key_on: :related
end
Add your child_model resource
# ./app/resources/child_model_resource.rb
class ChildModelResource < JSONAPI::Resource
end
Looks like the issue is here: https://github.com/cerebris/jsonapi-resources/blob/v0.7.0/lib/jsonapi/resource_serializer.rb#L307
With a cursory glance, it looks like the STI attribute is hard-coded to 'type', though the with the problem I am seeing personally, it could be that or a polymorphic association (unlikely, but maybe both). Either way, the issue can be reproduced with the setup below.
Also, may not be the best idea to assume the model has an :id field, instead you should be reading type from the parent's
inheritance_column
attribute and its primary_key from it'sprimary_key
attribute.If I get a chance today, I will look into fixing this, but thought I would report it in case I am unable to get to it before I forget about it lol
Create a new rails app:
Add jsonapi-resources
Add your routes
Add your controller
Add your parent model
Add your child model
Add your model that has_many child models
Add your has_many_child_models resource
Add your child_model resource
Now, start you server:
And navigate to:
To see the error: