cerebris / jsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.
http://jsonapi-resources.com
MIT License
2.32k stars 532 forks source link

How do I query an include on a belongs_to Single Table Inheritance association? #677

Closed terenceponce closed 8 years ago

terenceponce commented 8 years ago

I have the following models:

class Product < ApplicationRecord
  belongs_to :platform
end

class Software < Product
end

class Platform < ApplicationRecord
  has_many :products
end

and the following resources:

class ProductResource < JSONAPI::Resource
  attributes :name, :description

  relationship :platform, to: :one
end

class SoftwareResource < ProductResource
end

class PlatformResource < JSONAPI::Resource
  attributes :name

  relationship :products, to: :many
end

and this route:

Rails.application.routes.draw do
  jsonapi_resources :products
end

Also, I have these records in the database:

platform = Platform.create(name: 'Platform 1')
Software.create(name: 'Software 1', description: 'This is a software', platform: platform)

Doing curl http://localhost:3000/v1/products/1?include=platform -H "Content-Type: application/json" produces the following error:

Internal Server Error: PG::UndefinedColumn: ERROR:  column platforms.software_id does not exist.

How do I make JSONAPI::Resources query for products.platform_id instead? It doesn't make sense that it's querying for platforms.software_id when this is a belongs_to association.

terenceponce commented 8 years ago

Apparently, this doesn't have anything to do with JSONAPI::Resources. I'll just close this ticket.