-
Notifications
You must be signed in to change notification settings - Fork 526
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
has_many
with different public field
#29
Comments
Do you have a RecordResource declared? You will need that if you don't have it. If you do could you post that as well? You shouldn't need the |
@lgebhardt updated; I added that the model is actually namespaced under |
I haven't tested the code with namespaced classes. Could you put together a small sample app that reproduces the problem? Then I'll look at what it will take to support that case. |
Sure can |
@lgebhardt alright its up on jsmestad/jsonapi-resources-test-failure. If you run
|
@jsmestad Try: class DomainResource < JSONAPI::Resource
attributes :id, :name
has_many :records, key: 'dns_record_ids'
def id
@model.guid
end
end
class RecordResource < JSONAPI::Resource
attributes :id, :name
model_name 'Dns::Records'
def id
@model.guid
end
end The key needs to be the key that's used on the underlying model. Since the RecordResource isn't backed by a model named Record you need to tell it the name of the backing model with the model_name method. I'm glad to see that it works with the namespace despite the fact that I never thought to test it. Let me know it that works for you. |
@lgebhardt so that solves that error. Now for the follow up. I want to have turning this:
into this:
|
Glad that worked. I won't be able to look the id issue until later tonight or first think in the morning. |
alright; thanks again for helping out. I've been sifting through the codebase to find where to patch it but I cannot even come up with where this gets called ;) |
Dug in a bit more. It appears the culprit line is resource_serializer.rb It is relying on the ActiveRecord association's Temporary workaround pushed to the branch: workaround My thought is the default for |
You could rename your method to record_guids if you set the class DomainResource < JSONAPI::Resource
attributes :id, :name
has_many :records, key: :record_guids
def id
@model.guid
end
def record_guids
@model.dns_records.collect(&:guid)
end
end I'll need a bit of time to look at the implications of |
It looks like |
What about an |
I think you've exposed a flaw in the way JR handles keys. I'm going to experiment with a few things to see if I can improve the way JR deals with keys. We should be able to specify the primary key on the resource and use that whenever we represent the resource. If I get that working I still think you will need to implement a workaround, but it will be on the finders since those won't use the GUIDs. |
@jsmestad I just released version 0.0.8, which reworks the key option in resources and associations. I also put a branch of your test-failure app up at https://github.com/lgebhardt/jsonapi-resources-test-failure/tree/jr_key_changes that shows how you can use the new version of JR to do what you are trying to do with UUIDs. |
Closing this now since it should be fixed. Please reopen if it isn't fixed. |
I'm having issues using the
has_many
association helper and want to know if I am doing some obviously incorrect before I explore putting together a PR.Here is an example of the models. We use normal integer primary keys internally, but externally we only display the UUID.
This is what we are trying to do:
However this is not working. Any tips? Is this a use-case that is unsupported and I should put together a PR?
The text was updated successfully, but these errors were encountered: