Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polymorphic association on JSON API #1448

Closed
joaomdmoura opened this issue Jan 20, 2016 · 5 comments
Closed

Polymorphic association on JSON API #1448

joaomdmoura opened this issue Jan 20, 2016 · 5 comments

Comments

@joaomdmoura
Copy link
Member

When using JSON-API with polymorphic associations there is a strange behavior, instead of using the name of the resource it chooses to use the name assigned to the polymorphic relation.

This is not wrong ©️, but if you think about the most of JS clients frameworks around, it makes more sense to use the resource's name.

Here is as example to illustrate:

model/address.rb

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true
end

model/user.rb

class Address < ActiveRecord::Base
  has_one :home_address, as: :addressable, class_name: 'Address', dependent: :destroy
end

The serialized JSON output for address will be:

'data': {
  'id': 1,
  'type': 'addresses',
  'attributes': {
    ...
  },
  'relationships':  {
    'addressable': {
      'data': {
        'id': 1,
        'type': 'users'
      }
    }
  }
}

My point is related to this part:

  'relationships':  {
    'addressable': {
      'data': {
        'id': 1,
        'type': 'users'
      }
    }
  }
}

Despite of being a user the root name of this node is addressable, and IMO it should be user.

@beauby
Copy link
Contributor

beauby commented Jan 20, 2016

I agree. We might need a polymorphic relationship option as described in #1420.

@beauby
Copy link
Contributor

beauby commented Jan 20, 2016

cf #1453

@joaomdmoura
Copy link
Member Author

Checked both! Nice!

@beauby
Copy link
Contributor

beauby commented Jan 24, 2016

What happens when the addressable association is nil? What name should we give it?
What happens with heterogeneous has_many polymorphic associations?

@remear remear closed this as completed Mar 17, 2016
@cgmckeever
Copy link
Contributor

Realize this is closed, but I am fighting a similar issue around the naming of polymorphic associations. No offense @joaomdmoura, but I feel the way the json-api is set up is correct. There may be multiple of those polymorphs, so nesting them under 'addressable' and then defining what type it is gives the flexibility needed.

It appears that this behavior stopped happening for the JSON adapter circa rc3
#1420 (comment)

And, again, I feel this is limited ... I also have a issue open around this demonstrating more #1717

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants