-
Notifications
You must be signed in to change notification settings - Fork 182
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
Resource#find Returns Arrays #75
Comments
This was intentional for a few reasons: First, I always wanted to make a result set be available because there is extra meta data that comes back with the response. We could embed the meta data in a single resource object but that seems weird because it wouldn't be available on the resource objects fetched in a multi object response. I guess we could store a reference to the meta data but that also seems a bit janky. Second, in the initial version of jsonapi, all responses returned arrays - even requests for a single resource. The rationale behind that was to make client side parsing easier - no matter what you were fetching, you could always iterate of the results like an array. There was a change (about a year ago?) to the spec that brought back the ability to respond with a single object or and array of objects as the main data. I wasn't a huge fan of it at the time. Additionally, I really don't like the behavior in ActiveRecord where Hope this gives some insight into why it's doing this. |
I would be open to doing away with |
there can be another method for easy fetching single resource, changing find behaviour is breaking change , don't think is good idea. |
Thanks for taking the time to get back to me on this. I'm looking to mimic the API's way of representing data more closely. Right now the Ideally, there would be a clean way of separating the two while preserving ease of use. I see It is clearly a breaky change, and a different name might be best way to go. |
I'm wondering why this issue was closed, since I can't find any method offering the suggested behaviour. Currently I'm using my own class My::QueryBuilder < ::JsonApiClient::Query::Builder
def find(args = {})
args.is_a?(Hash) ? super : super.first
end
end Maybe this helps someone else or @jakesower. |
The way it works now, It's possible to add a |
@chingor13 thanks for your feedback. I would like to give you my thoughts here as well. I see your point in consistency with Since I'm requesting the model, I expect to get the model and not some metadata with it. I would need it only in very rare situations. For me it would make more sense to not have the metadata returned per default, but the model-data. So methods like And not to forget: Thanks for your time and work you put into this gem! |
You may want to use json_api_resource in conjunction with json_api_client. It provides that find behavior and allows custom methods on your API objects on the application side. |
@sringling: I'm using @chingor13 Do you see any chance to make BTW: Would be great if you could reopen the issue, since there is still no easy solution. |
I am curious why .first is is required in this snippet (from the README):
It makes more sense to me to do something like:
This is intentional behavior: https://github.com/chingor13/json_api_client/blob/master/lib/json_api_client/parsers/parser.rb#L62
I will be changing this logic in my fork of the repository to make it work like the second snippet above. I would be interested in the thinking behind that choice, and/or if there would be interest in merging the change from my branch into this one.
The text was updated successfully, but these errors were encountered: