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

Non-standard source for a relationship #136

Closed
kbaird opened this issue Oct 2, 2018 · 6 comments
Closed

Non-standard source for a relationship #136

kbaird opened this issue Oct 2, 2018 · 6 comments

Comments

@kbaird
Copy link
Contributor

kbaird commented Oct 2, 2018

Hi. Let's say I have a Product, with a tag_ids field of type {:array, :string}.

I'd like to construct a normal-appearing tags relationship in my ProductView, such that the tags derive not from a regular has_many relationship, but rather any Tag whose uuid primary_key is in that product.tag_ids array.

Is there any reasonable way to do this?

@kbaird
Copy link
Contributor Author

kbaird commented Oct 3, 2018

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.

@jeregrine
Copy link

jeregrine commented Oct 3, 2018 via email

@kbaird
Copy link
Contributor Author

kbaird commented Oct 3, 2018

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.

@kbaird
Copy link
Contributor Author

kbaird commented Oct 3, 2018

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.

@kbaird
Copy link
Contributor Author

kbaird commented Feb 8, 2019

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

@jherdman jherdman mentioned this issue Feb 8, 2019
2 tasks
@jherdman
Copy link
Contributor

jherdman commented Feb 8, 2019

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

@jherdman jherdman closed this as completed Feb 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants