{"action":"closed","issue":{"url":"https://api.github.com/repos/elixir-lang/ecto/issues/1001","labels_url":"https://api.github.com/repos/elixir-lang/ecto/issues/1001/labels{/name}","comments_url":"https://api.github.com/repos/elixir-lang/ecto/issues/1001/comments","events_url":"https://api.github.com/repos/elixir-lang/ecto/issues/1001/events","html_url":"https://github.com/elixir-lang/ecto/issues/1001","id":110584971,"number":1001,"title":"different embed schemas for the same model (possible pull request)","user":{"login":"troyk","id":13517,"avatar_url":"https://avatars.githubusercontent.com/u/13517?v=3","gravatar_id":"","url":"https://api.github.com/users/troyk","html_url":"https://github.com/troyk","followers_url":"https://api.github.com/users/troyk/followers","following_url":"https://api.github.com/users/troyk/following{/other_user}","gists_url":"https://api.github.com/users/troyk/gists{/gist_id}","starred_url":"https://api.github.com/users/troyk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/troyk/subscriptions","organizations_url":"https://api.github.com/users/troyk/orgs","repos_url":"https://api.github.com/users/troyk/repos","events_url":"https://api.github.com/users/troyk/events{/privacy}","received_events_url":"https://api.github.com/users/troyk/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-10-09T04:11:32Z","updated_at":"2015-10-09T05:01:06Z","closed_at":"2015-10-09T05:01:06Z","body":"I have the following schema:\r\n```elixir\r\ndefmodule AH.SearchReq do\r\n  use AH.Web, :model\r\n\r\n  schema \"search_reqs\" do\r\n    field :product_id, :string\r\n    field :request, :map\r\n    field :result, :map\r\n  end\r\nend\r\n```\r\n\r\nI want to use different embed schema's based on the value of `product_id` and embed them in the request and result :map fields.  At first, I thought the `after_load` callback would work because I could do a cond on the product_id and return a different model.  But I get this error:\r\n\r\n```\r\n** (exit) an exception was raised:\r\n    ** (ArgumentError) expected `after_load` callbacks to return a AH.SearchReq, got: %AH.SearchReq.CRCT{__meta__: #Ecto.Schema.Metadata<:built>, ...}\r\n        (ecto) lib/ecto/model/callbacks.ex:319: Ecto.Model.Callbacks.__apply__/3\r\n        (ecto) lib/ecto/adapters/sql.ex:503: anonymous fn/3 in Ecto.Adapters.SQL.process_row/3\r\n        (elixir) lib/enum.ex:1102: Enum.\"-map_reduce/3-lists^mapfoldl/2-0-\"/3\r\n        ...\r\n```\r\n\r\nIf there is not a good way to do this, any interest in a pull-request to make after_load not require the same model to be returned?  There does not appear to be any code after the `Schema.__load__/6` that depends on the same model being and while I understand the other callbacks require a changeset to be returned, is there really a reason to ensure after_load keeps the same model?\r\n```elixir\r\n  def __load__(model, prefix, source, context, data, loader) do\r\n    source = source || model.__schema__(:source)\r\n    struct = model.__struct__()\r\n    fields = model.__schema__(:types)\r\n\r\n    loaded = do_load(struct, fields, data, loader)\r\n    loaded = Map.put(loaded, :__meta__,\r\n                     %Metadata{state: :loaded, source: {prefix, source}, context: context})\r\n    Ecto.Model.Callbacks.__apply__(model, :after_load, loaded)\r\n  end\r\n```"}}