Giter Site home page Giter Site logo

Comments (5)

lgebhardt avatar lgebhardt commented on August 18, 2024

I was just starting to look at this. What was the resolution?

from jsonapi-resources.

7h3kk1d avatar 7h3kk1d commented on August 18, 2024

I reworked something on my end so it didn't matter anymore. I didn't want to create more work if no one else was complaining.

from jsonapi-resources.

lgebhardt avatar lgebhardt commented on August 18, 2024

Ok, thanks. I'll still take a look at it then.

from jsonapi-resources.

lgebhardt avatar lgebhardt commented on August 18, 2024

Were you seeing the normal Rails behavior of not validating the existence of a related object? If so, there are a few ways you can fix that. The first is using a validation gem such as https://github.com/perfectline/validates_existence, which I have not tested. Another way would be to put foreign key constraints on the database and handle the error when the resource is saved. And finally you could override the verify_key method on your resource (or base_resource). For example:

def verify_key(key, context = nil)
  #try to load a resource to make sure it exists. Will raise a JSONAPI::Exceptions::RecordNotFound if the resource is not found
  find_by_key(key, context: context)
  return key
end

I personally like overriding the verify_key method, however if you use your models outside of jsonapi-resources one of the other approaches might be better. I'm also a firm believer in foreign keys as an additional safety measure.

One final note, using the verify_key method can also provide a way to check if the current user has permission to see the related record since the context can be taken into account during the find_by_key method.

from jsonapi-resources.

7h3kk1d avatar 7h3kk1d commented on August 18, 2024

Okay. Thanks very much! I added the database constraint.

from jsonapi-resources.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.