How do I return a property value from an association rather than a nested object?

I am working on using Phoenix for a JSON API and I currently have two models - Country and Continent. A Country must belong to a Continent.

I have the association working but when I make a request it returns a nested object rather than a property value. So the return currently looks like:

   {
            "continent": {
                "id": 1,
                "name": "Europe"
            },
            "id": 1,
            "name": "England"
        },

I would ideally prefer it to be:

   {
            "continent": "Europe"
            "id": 1,
            "name": "England"
        },

I assume this is due to the way I retrieve the association in the view using render_one/3

def render("country.json", %{country: country}) do
    %{id: country.id,
      name: country.name,
      continent: render_one(country.continent, FcareersWeb.ContinentView, "continent.json")}
  end

But I’m unsure how to render the association without using a render function. I feel like I’m missing something obvious and any help would be appreciated.

Replace render_one(country.continent, FcareersWeb.ContinentView, "continent.json") with country.continent.name.

4 Likes

Well that is embarrassing, thanks @LostKobrakai