You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-5Lines changed: 36 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,14 +52,14 @@ u.update_attributes(
52
52
c:"d"
53
53
)
54
54
55
-
u.persisted?
55
+
u.persisted?
56
56
# => true
57
57
58
58
u.destroy
59
59
60
-
u.destroyed?
60
+
u.destroyed?
61
61
# => true
62
-
u.persisted?
62
+
u.persisted?
63
63
# => false
64
64
65
65
u =MyApi::Person.create(
@@ -164,7 +164,7 @@ module MyApi
164
164
classAccount < JsonApiClient::Resource
165
165
belongs_to :user
166
166
end
167
-
167
+
168
168
classCustomer < JsonApiClient::Resource
169
169
belongs_to :user, shallow_path:true
170
170
end
@@ -476,7 +476,7 @@ end
476
476
477
477
##### Custom status handler
478
478
479
-
You can change handling of response status using `connection_options`. For example you can override 400 status handling.
479
+
You can change handling of response status using `connection_options`. For example you can override 400 status handling.
480
480
By default it raises `JsonApiClient::Errors::ClientError` but you can skip exception if you want to process errors from the server.
481
481
You need to provide a `proc` which should call `throw(:handled)` default handler for this status should be skipped.
482
482
```ruby
@@ -636,6 +636,37 @@ end
636
636
637
637
```
638
638
639
+
### Safe singular resource fetching
640
+
641
+
That is a bit curios, but `json_api_client` returns an array from `.find` method, always.
642
+
The history of this fact was discussed [here](https://github.com/JsonApiClient/json_api_client/issues/75)
643
+
644
+
So, when we searching for a single resource by primary key, we typically write the things like
645
+
646
+
```ruby
647
+
admin =User.find(id).first
648
+
```
649
+
650
+
The next thing which we need to notice - `json_api_client` will just interpolate the incoming `.find` param to the end of API URL, just like that:
651
+
652
+
> http://somehost/api/v1/users/{id}
653
+
654
+
What will happen if we pass the blank id (nil or empty string) to the `.find` method then?.. Yeah, `json_api_client` will try to call the INDEX API endpoint instead of SHOW one:
655
+
656
+
> http://somehost/api/v1/users/
657
+
658
+
Lets sum all together - in case if `id` comes blank (from CGI for instance), we can silently receive the `admin` variable equal to some existing resource, with all the consequences.
659
+
660
+
Even worse, `admin` variable can equal to *random* resource, depends on ordering applied by INDEX endpoint.
661
+
662
+
If you prefer to get `JsonApiClient::Errors::NotFound` raised, please define in your base Resource class:
663
+
664
+
```ruby
665
+
classResource < JsonApiClient::Resource
666
+
self.raise_on_blank_find_param =true
667
+
end
668
+
```
669
+
639
670
## Contributing
640
671
641
672
Contributions are welcome! Please fork this repo and send a pull request. Your pull request should have:
0 commit comments