Giter Club home page Giter Club logo

Comments (6)

kbaird avatar kbaird commented on September 3, 2024

In case anyone's curious, a potential solution in the schema file:
field :tags, {:array, :map}, virtual: true

  def inject_tags_from_tag_ids(product) do
    tag_ids = product.tag_ids || []
    tags = for id <- tag_ids, do: %{id: id}
    %{product | tags: tags}
  end

Then adding tags: TagView to relationships in the View seems to work OK, as long as you pass your product through the function at some point.

NOTE: edited to remove incorrect :include from View code example.

from jsonapi.

jeregrine avatar jeregrine commented on September 3, 2024

from jsonapi.

kbaird avatar kbaird commented on September 3, 2024

Just spitballing on things that would've seemed easier to me.

here's one idea:

def relationships do
  [
    # other entries...
    tags: {:tag_ids, &tags_from_ids/1}
  ]
end

defp tags_from_ids(tag_ids) do
  for id <- tag_ids, do: %{id: id, type: "tags"}
end

The idea being that :tag_ids is the name of the field that is the source of the data, and &tags_from_ids/1 is the function to transform said data.

The particulars above could be modified to allow specifying TagView somewhere as well, etc. If that's easier.

from jsonapi.

kbaird avatar kbaird commented on September 3, 2024

You could also lock it down more with

def relationships do
    [
    # other entries...
    tags: [ids: :tag_ids, type: "tags"]
  ]
end

That's less flexible, intended only for an input that is known to be a list of IDs (as is true for us). I'm not sure what else you might be getting in for a relationship, anyway.

from jsonapi.

kbaird avatar kbaird commented on September 3, 2024

FYI, this is no longer a priority for our specific project.

from jsonapi.

jherdman avatar jherdman commented on September 3, 2024

Ha! I was just contemplating this issue. Many thanks for the heads up. I'll close this issue then.

from 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.