Code Monkey home page Code Monkey logo

Comments (11)

shishirmk avatar shishirmk commented on July 17, 2024

@vovimayhem That is a very nice way to refactor some of the repetition we have in the code today. You are right as far JSON:API is concerned i don't think there is any distinction between belongs_to and has_one. I think we should work on this idea.

from fast_jsonapi.

amrrbakry avatar amrrbakry commented on July 17, 2024

I think I can help with this. I will open a PR soon 👍

from fast_jsonapi.

vovimayhem avatar vovimayhem commented on July 17, 2024

Hey
I've got a commit with that on another PR (Sorry!)

from fast_jsonapi.

vovimayhem avatar vovimayhem commented on July 17, 2024

c5295e6

from fast_jsonapi.

amrrbakry avatar amrrbakry commented on July 17, 2024

No problem. Good work 👍

from fast_jsonapi.

dillonwelch avatar dillonwelch commented on July 17, 2024

Fixed in #188, @shishirmk can you close?

from fast_jsonapi.

shishirmk avatar shishirmk commented on July 17, 2024

This should be released in version 1.2 of the gem

from fast_jsonapi.

ababich avatar ababich commented on July 17, 2024

I think this is a big problem when has_one and belongs_to are aliased they are logically different and it should be implemented:

A belongs_to B means A has b_id attribute
A has_one B means B has a_id attribute

Current implementation when you have to provide additional attribute for has_one relation is pretty inconvenient

from fast_jsonapi.

abevoelker avatar abevoelker commented on July 17, 2024

I agree with @ababich and created #257 for consideration

from fast_jsonapi.

vovimayhem avatar vovimayhem commented on July 17, 2024

@ababich @abevoelker IMHO the direction/cardinality of the association in the database shouldn't be relevant on the serialization part of JSONAPI.

In both cases, you get the associated object's id and type, regardless if the ActiveRecord/ORM model your'e serializing has it implemented as a belongs_to or a has_one. There should be no extra attributes/options required to declare on the serializer.

On an additional note, since we need to traverse the object's association to know the real type of an associated object (via a has_one on the serializer), on most cases the same way to retrieve the associated id (via object.association&.id) will be used.

from fast_jsonapi.

ababich avatar ababich commented on July 17, 2024

@vovimayhem

fast_jsonapi always expects _id field in model A which is inconvenient in lots of one-to-one associations

for example:

we have base model A and with one-to-one relation with model AHeavyDatails and model AStats

both AHeavyDatails and AStats have a_id field

Having a_id in "extension" models is more scalable, etc. but, anyway, this is existing DB design

to describe this design, belongs_to and has_one are used in all popular ORM which helps to describe this logic

I see no reason why fast_jsonapi cannot follow similar convention

what's more, in terms of API it is very convenient to requiest model A and optionally include AHeavyDatails and/or AStats from example above.

from fast_jsonapi.

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.