Skip to content
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

Issue with parsing errors on associated models #110

Open
glappen opened this issue Aug 13, 2019 · 4 comments
Open

Issue with parsing errors on associated models #110

glappen opened this issue Aug 13, 2019 · 4 comments

Comments

@glappen
Copy link

glappen commented Aug 13, 2019

Hello,

I just encountered an issue where my API returns errors for an associated object. Let's say I have a User model, and a User has-many UserRoles. The errors json returned from my API looks like this:

{:errors=>
  {
    :email=>[
      {:error=>"blank"}
    ], 
    :"user_roles.years_of_experience"=>[{:error=>"blank"}]
  }
}

That is what I get when I call render json: { errors: @user.errors.details } in my API.

The error I get from Spyke is:

undefined method `user_roles.years_of_experience' for #<CoreClient::User:0x00007f1868008a70>
spyke (5.3.4) lib/spyke/attribute_assignment.rb:101:in `method_missing'
activemodel (5.2.2.1) lib/active_model/errors.rb:420:in `generate_message'
activemodel (5.2.2.1) lib/active_model/errors.rb:454:in `normalize_message'
activemodel (5.2.2.1) lib/active_model/errors.rb:298:in `add'
spyke (5.3.4) lib/spyke/http.rb:108:in `block (2 levels) in add_errors_to_model'

Is there any support for Spyke parsing errors for associated objects, or is it only working for attributes one level deep in the model being created? Maybe I'm not putting errors hash into the correct format.

Thanks!

Greg

@balvig
Copy link
Owner

balvig commented Aug 19, 2019

@glappen looks like maybe you have indeed stumbled on a bug! 😢
I believe the hash is in the correct format, but Spyke is not able to handle nested errors properly yet 🤔

@glappen
Copy link
Author

glappen commented Aug 19, 2019 via email

@glappen
Copy link
Author

glappen commented Aug 19, 2019

@balvig I have spent some time reviewing the spyke code, but I am still not sure how best to add this support. Do you have some suggestions on where to start? Thanks!

@balvig
Copy link
Owner

balvig commented Oct 4, 2019

@glappen I've added a failing spec here: master...jb/nested-errors

The problem seems to be that while Spyke uses ActiveModel to add errors onto the model:

errors.add(field.to_sym, error.message, error.options)

...it however doesn't seem to support the xxx.yyy shortcut for adding errors to associations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants