tiagopog/jsonapi-utils

One To One Relation; relation_link is broken

peco8 opened this issue · 4 comments

peco8 commented

Hi !!
I have one to one relation between model such as

class Plan < ActiveRecord::Base
  has_one :subscription
end

class Subscription < ActiveRecord::Base
  belongs_to :plan
end

So that I could define resources like,

class PlanResource < JSONAPI::Resource
  immutable

  attributes :uuid,
             :amount,
             :name,
             :created_at,
             :updated_at

  has_one :subscription, class_name: 'Subscription'
end

class SubscriptionResource < JSONAPI::Resource
  immutable

  attributes :stripe_subscription_id,
             :created_at,
             :updated_at

  has_one :plan, class_name: 'Plan', foreign_key: 'plan_id'
end

And then when I send the request to
http://localhost:3000/plans/1/relationships/subscription.
I can see the errors below;

{
  "errors": [
    {
      "title": "Internal Server Error",
      "detail": "Internal Server Error",
      "id": null,
      "href": null,
      "code": 500,
      "source": null,
      "links": null,
      "status": "500",
      "meta": {
        "exception": "undefined method `subscription_id' for class `#<Class:0x007ff5e0e4ffb0>'",
        "backtrace": [
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/resource.rb:804:in `method'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/resource.rb:804:in `block (2 levels) in _add_relationship'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/resource_serializer.rb:300:in `public_send'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/resource_serializer.rb:300:in `foreign_key_value'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/resource_serializer.rb:249:in `to_one_linkage'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/resource_serializer.rb:61:in `serialize_to_links_hash'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/response_document.rb:113:in `results_to_hash'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/response_document.rb:11:in `contents'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/acts_as_resource_controller.rb:155:in `render_results'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/acts_as_resource_controller.rb:64:in `process_request'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/jsonapi-resources-0.7.0/lib/jsonapi/acts_as_resource_controller.rb:21:in `show_relationship'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/abstract_controller/base.rb:198:in `process_action'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/action_controller/metal/rendering.rb:10:in `process_action'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/abstract_controller/callbacks.rb:20:in `block in process_action'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:117:in `call'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:505:in `call'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:92:in `__run_callbacks__'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:81:in `run_callbacks'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/abstract_controller/callbacks.rb:19:in `process_action'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/action_controller/metal/rescue.rb:29:in `process_action'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/notifications.rb:164:in `block in instrument'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/notifications.rb:164:in `instrument'",
          "/Users/toshikiinami/Desktop/billing/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'",

When I used to use JR the result is following:

{
  "links": {
    "self": "http://localhost:3000/plans/1/relationships/subscription",
    "related": "http://localhost:3000/plans/1/subscription"
  },
  "data": null
}

Maybe the controller is not generated automatically? or I've been missing something?

screen shot 2017-01-29 at 11 27 52

**I removed api/v1 in the path for the sake of clarity within the code above. **

Hey there!

Well, as far as I can see it isn't touching JU actually, so I suppose you are probably using the default relationship action provided by JR in your plan controller, right?

Taking a look at the data and the stack trace you provided, it seems that  JR doesn't find the subscription_id method and then it tries to define/call it in the model of your plan resource, treating the relationship erroneously as if the plan belongs to the subscription, which is not true.

  1. Can you pry that line on JR and double check the value of @model?
  2. Can you provide the payload of that request?

I'm not sure if I can manage to solve this issue, maybe it's something more suitable to be opened on JR, but let's try :-)

peco8 commented

Hi tiagopog,

Thanks for the reply!
I checked what I edited so far but nothing really changed so far.

Then I checked my json-api-resources version. It downgraded from v0.8.0 to v0.7.0 with JU.
I literally swapped the version to v0.7.0 current development branch using JR, and same errors happened.

As you mentioned before, v0.7.0 and v0.8.0 seems to be changed a lot.
v0.7.0 => https://github.com/cerebris/jsonapi-resources/blob/bf2705b55af414576f25d30e57e6ee5eda3f94b3/lib/jsonapi/resource.rb#L801
v0.8.0 => https://github.com/cerebris/jsonapi-resources/blob/v0.8.0/lib/jsonapi/resource.rb

JR doesn't find the subscription_id method and then it tries to define/call it in the model of your plan resource, is totally true.

Could you please suggest how I could workaround this ?

I use rails 4.3.

Hey there, @peco8! Are you still experiencing such a problem? If you've fixed it can you please provide a resolution of the error?

I've been going through very busy days lately and since it seems the problem is not related to JU I ended up not giving much attention to this issue, but I'd really like to know how/if you've solved it.

Cheers!

Closed by #54