-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Problem with entity in :using #75
Comments
I got over this issue by using expose :preferences do |model, options|
if model.preference
# Hash
end
end instead of But I think its still an issue! |
I believe I've run into this issue, myself. I can't seem to find a way leverage :using in the way that the documentation seems to intend its usage: nesting entities. Grape::Entity implements #as_json, but when JSON encoders like OkJson go to encode the nested Entity objects, it tries to call #to_json (which does not exist). Depending on the JSON encoder, they may chose to encode an object that lacks #to_json, however OkJson throws an exception if it does not. I've effectively done the following in my code in order to encode the entities in a consistent manner.
Alternatively, doing something like the following also works, but I suspect that it is a bit inappropriate.
I haven't looked into it more deeply, but I may see if I can't figure out a patch and send a pull request to fix it. |
For me the :using property is also not working. I think its because of the missing to_json function or something like that: [
{
...
"slots": [
"#<Harbour::API::V1::SlotEntity:0x007fcb33d243c0>",
"#<Harbour::API::V1::SlotEntity:0x007fcb33d24230>",
"#<Harbour::API::V1::SlotEntity:0x007fcb33d240f0>",
"#<Harbour::API::V1::SlotEntity:0x007fcb33d23fb0>",
"#<Harbour::API::V1::SlotEntity:0x007fcb33d23dd0>"
]
}
] To fix the nesting problem i'm using currently the following method: expose :slots do |station,options|
station.slots.map{ |slot| SlotEntity.new(slot).serializable_hash }
end |
Similar fix for a has_one relationship:
Trying it the recommended way resulted in the following error:
|
I have the same issue. The strangest thing is that it works on my laptop but breaks on travis-ci (https://travis-ci.org/lucassus/mongo_browser/jobs/3839962) and on my other machine. On both machines I have the same ruby's configuration: ruby-1.9.3-p327-perf with separate rvm gemsets for my project. |
This may be fixed on HEAD. We now fully reply on the formatter, so if you use |
@agileanimal Does this bug belong in grape-entity? |
What's the best way to move it over and keep the thread? |
@amrnt Do you have any updates to this? Is it a problem still? Can I close this? |
I am closing this. |
Assume I have 2 models: User and Preference. User
has_one
preference.And here's Grape entities:
Now, with
Throws this error:
The text was updated successfully, but these errors were encountered: