2014-11-06 12 views
7

Vorrei generare JSON da un'associazione ecto in phoenix.creazione json da elixir associazioni ecto

questa è la mia associazione:

defmodule Blog.Post do 
    use Ecto.Model 

    schema "posts" do 
    field :title, :string 
    field :body, :string 
    has_many :comments, Blog.Comment 
    end 
end 

e:

defmodule Blog.Comment do 
    use Ecto.Model 

    schema "comments" do 
    field :content, :string 
    belongs_to :post, Blog.Post 
    end 
end 

quando ho generare JSON, senza l'associazione Il risultato è questo:

[%Blog.Post{body: "this is the very first post ever!", id: 1,title: "first post"}, 
%Blog.Post{body: "Hello nimrod!!!!", id: 12, title: "hi Nimrod"}, 
%Blog.Post{body: "editing the body!!!!", id: 6, title: "hello(edit)"}] 

e JSON assomiglia a questo

{"posts": [ 
    { 
     "title": "first post", 
     "id": 1, 
     "body": "this is the very first post ever!" 
    }, 
    { 
     "title": "hi Nimrod", 
     "id": 12, 
     "body": "Hello nimrod!!!!" 
    }, 
    { 
     "title": "hello(edit)", 
     "id": 6, 
     "body": "editing the body!!!!" 
    } 
]} 

ma con l'associazione del risultato è questo

[%Blog.Post{body: "this is the very first post ever!", 
comments: {Ecto.Associations.HasMany.Proxy, 
#Ecto.Associations.HasMany<[name: :comments, target: Blog.Post, 
associated: Blog.Comment, references: :id, foreign_key: :post_id]>}, id: 1, 
title: "first post"}, 
%Blog.Post{body: "Hello nimrod!!!!", 
comments: {Ecto.Associations.HasMany.Proxy, 
#Ecto.Associations.HasMany<[name: :comments, target: Blog.Post, 
associated: Blog.Comment, references: :id, foreign_key: :post_id]>}, id: 12, 
title: "hi Nimrod"}, 
%Blog.Post{body: "editing the body!!!!", 
comments: {Ecto.Associations.HasMany.Proxy, 
#Ecto.Associations.HasMany<[name: :comments, target: Blog.Post, 
associated: Blog.Comment, references: :id, foreign_key: :post_id]>}, id: 6, 
title: "hello(edit)"}] 

Con l'uscita di cui sopra non può creare un'uscita json corretta. Vorrei che Json assomigliasse a qualcosa del genere

{"posts": [ 
    { 
     "title": "the title", 
     "id": 1, 
     "body": "the body", 
     "comments": [{"content": "a comment"}, {"content": "another comment"}] 
    } 
    ... 
]} 

Qualsiasi aiuto sarebbe apprezzato.

+0

Quale libreria si usa per generare il JSON? Si prega di aggiungere uno snippet di esempio. –

+0

È la libreria phoenix predefinita, Poison: 'Poison.encode! % {post: Blog.Repo.all (Blog.Post)} ' – idobn

+0

Suppongo che tu abbia ricevuto un errore, vero? Puoi per favore postare l'errore qui? –

risposta

6

Ouch, non esiste una soluzione facile in questo momento. Vorrei provare qualcosa di simile:

defimpl Poison.Encoder, for: Tuple do 
    def encode(proxy, options) do 
    Poison.Encoder.List.to_json(proxy.all, options) 
    end 
end 

siamo fondamentalmente attuando encoder per tuple che riceve la delega di cui sopra e la codifica di tutti gli elementi. Dobbiamo discutere di soluzioni migliori per questo.

+0

Non sono sicuro di capire esattamente cosa stai facendo ... Potresti forse mostrare un esempio di come lo faresti. – idobn

+1

Questo è un ostacolo all'impossibilità delle associazioni di rendering di Ecto. Dovresti aggiungerlo a uno dei file del tuo progetto e dovrebbe essere sufficiente, a meno che non manchi qualcosa, ma il proxy nel codice sopra riportato è il valore restituito da "post.comments". Il razionale è che il proxy di associazione sia una tupla, quindi stai insegnando a Poison come renderli a JSON. –

+0

Ho provato ad aggiungere questo al mio progetto e ho ricevuto il seguente avviso: 'avviso: codifica funzione del protocollo non definita/2 (per protocollo Poison.Encoder)' e dopo aver provato a eseguire 'Poison.encode! Blog.Repo.all (Blog.Post) 'Ho ricevuto questo errore: Funzione di' ** (UndefinedFunctionError) undefined: Poison.Encoder.Tuple.encode/2 (blog) Poison.Encoder.Tuple.encode ({Ecto.Associations .HasMany.Proxy, # Ecto.Associations.HasMany <[nome:: commenti, target: Blog.Post, associato: Blog.Comment, riferimenti:: id, foreign_key:: post_id]>}, []) ' – idobn

Problemi correlati