-
Notifications
You must be signed in to change notification settings - Fork 66
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
Invalid path error thrown too early for URIs with other IDs #90
Comments
Thanks for submitting the issue @paulvt! Would it be possible to include the JSON responses for the examples? I think the Even better if you can reproduce the problem with a test, for example taking inspiration from https://github.com/balvig/spyke/blob/master/test/associations_test.rb |
Ok, the problem seems to be unrelated to chaining/nesting of relations (and thus #89). The issue seems to be that an The test to show what I mean is as follows: def test_return_nil_for_missing_id
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { id: 1, user_id: "1" } )
stub_request(:get, 'http://sushi.com/recipes/2').to_return_json(result: { id: 2, user_id: nil } )
stub_request(:get, 'http://sushi.com/users/1').to_return_json(result: { id: 1 })
recipe = Recipe.find(1)
assert_equal 1, recipe.user.id
recipe = Recipe.find(2)
assert_nil recipe.user
end For recipe with ID 1, the user is known and retrievable. For recipe with ID 2, the user is unknown and no retrieval attempt should be made instead of throwing the exception. This should be analogue to when the data would have been embedded, |
@paulvt not sure if the approach I'm taking in https://github.com/balvig/spyke/compare/belongs_to is the best yet, but could you maybe give it a try to see if it solves your problem? |
Yeah, I already had an inkling that the URL was constructed before checking if the necessary attributes, in this case the primary key, were present. So, this seems good. I have also tested with the branch and this indeed fixes the problem! |
Awesome, thanks @paulvt! I will do some more testing and then look to releasing a new version. |
If the URI of a relation contains another (parent) ID, retrieving the object fails with a
Skype::InvalidPathError
before it is even checked whether the ID is notnil
. The expected behaviour would be to just returnnil
if the ID for the relation is not set. For example:More specifically, the last call raises the exception:
The text was updated successfully, but these errors were encountered: