【问题标题】:Get only specific fields when fetching data using Ecto in Phoenix在 Phoenix 中使用 Ecto 获取数据时仅获取特定字段
【发布时间】:2015-07-06 20:35:36
【问题描述】:

我正在尝试在 Phoenix 的一个 API 调用中返回一些 JSON 数据。我正在获取 Subject 的所有记录并发送它们,但 Ecto 返回一些我不想要的额外字段。

我能做什么:

  • 仅获取特定属性(例如仅idname
  • 在我的回复中没有得到不必要的字段(例如__meta____owner__

这是我的Controller

# Controller
def index(conn, _) do
    subjects = Subject |> Repo.all
    conn |> render subjects: subjects
end

这是我的View

# View
def render("index.json", %{subjects: subjects}) do
    subjects
end

这是我的回应:

[
    {
        "teachers": {
            "__owner__": "Elixir.MyApp.Subject",
            "__field__": "teachers",
            "__cardinality__": "many"
        },
        "updated_at": "2015-06-20T15:32:20Z",
        "topics": {
            "__owner__": "Elixir.MyApp.Subject",
            "__field__": "topics",
            "__cardinality__": "many"
        },
        "name": "Physics",
        "inserted_at": "2015-06-20T15:32:20Z",
        "id": 1,
        "__meta__": {
            "state": "loaded",
            "source": "subjects"
        }
    },
    {
        "teachers": {
            "__owner__": "Elixir.MyApp.Subject",
            "__field__": "teachers",
            "__cardinality__": "many"
        },
        "updated_at": "2015-06-20T15:37:59Z",
        "topics": {
            "__owner__": "Elixir.MyApp.Subject",
            "__field__": "topics",
            "__cardinality__": "many"
        },
        "name": "Chemistry",
        "inserted_at": "2015-06-20T15:37:59Z",
        "id": 2,
        "__meta__": {
            "state": "loaded",
            "source": "subjects"
        }
    },
    {
        "teachers": {
            "__owner__": "Elixir.MyApp.Subject",
            "__field__": "teachers",
            "__cardinality__": "many"
        },
        "updated_at": "2015-06-20T15:38:41Z",
        "topics": {
            "__owner__": "Elixir.MyApp.Subject",
            "__field__": "topics",
            "__cardinality__": "many"
        },
        "name": "Mathematics",
        "inserted_at": "2015-06-20T15:38:41Z",
        "id": 3,
        "__meta__": {
            "state": "loaded",
            "source": "subjects"
        }
    },
    {
        "teachers": {
            "__owner__": "Elixir.MyApp.Subject",
            "__field__": "teachers",
            "__cardinality__": "many"
        },
        "updated_at": "2015-06-22T15:40:17Z",
        "topics": {
            "__owner__": "Elixir.MyApp.Subject",
            "__field__": "topics",
            "__cardinality__": "many"
        },
        "name": "Biology",
        "inserted_at": "2015-06-22T15:40:17Z",
        "id": 4,
        "__meta__": {
            "state": "loaded",
            "source": "subjects"
        }
    }
]

【问题讨论】:

    标签: elixir phoenix-framework ecto


    【解决方案1】:

    将您的视图更改为:

    def render("index.json", %{subjects: subjects}) do
      Enum.map(subjects, &Map.take(&1, [:id, :name]))
    end
    

    此外,您还可以通过将控制器更改为:

    让 Ecto 返回字段子集:
    def index(conn, _) do
      subjects = from(s in Subject, select: %{id: s.id, name: s.name}) |> Repo.all
      conn |> render subjects: subjects
    end
    

    【讨论】:

    • 感谢您的回答。我了解如何选择特定字段,但是如果我想拒绝特定字段并获取其他所有内容怎么办。例如,如果我不想在上面的示例中使用teacherstopics
    • Map.take/2 更改为Map.drop/2。您可以在elixir-lang.org/docs/stable/elixir找到所有地图功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多