Skip to content

Instantly share code, notes, and snippets.

@cibernox
Last active August 29, 2015 14:27
Show Gist options
  • Save cibernox/1be2430901d081d46529 to your computer and use it in GitHub Desktop.
Save cibernox/1be2430901d081d46529 to your computer and use it in GitHub Desktop.
query = from student in Student,
where: student.type == "Student" and student.school_id == 1,
preload: [:class_groups, :parent_students] # <- from parent_students table I only care about the `parent_id`.
# Ideally I'd just like to have Mysql return me an array named `parent_ids`.
render conn, "index.json", students: Repo.all query
@elbow-jason
Copy link

  query = from student in Student,
  where: student.type == "Student" and student.school_id == 1,
  preload: [class_groups: from(cg in ClassGroups, select: cg.id) ],
  preload: [parent_students: from(ps in ParentStudents, select: ps.id) ]

render conn, "index.json", students: Repo.all query

@cibernox
Copy link
Author

      query = from student in Student,
      where: student.type == "Student" and student.school_id == 1,
      preload: [:class_groups],
      preload: [parent_students: from(ps in ParentStudent, select: ps.parent_id)]

Something is wrong here:

** (Ecto.Query.CompileError) `from(ps in ParentStudent, select: ps.parent_id())` is not a valid preload expression. preload expects an atom, a (nested) list of atoms or a (nested) keyword list with a binding, atoms or lists as values. Use ^ if you want to interpolate a value

@cibernox
Copy link
Author

defmodule SmhwPhoenix.StudentController do
  use SmhwPhoenix.Web, :controller
  alias SmhwPhoenix.Repo
  alias SmhwPhoenix.Student
  alias SmhwPhoenix.ParentStudent

  def index(conn, params) do
    query = from student in Student,
      where: student.type == "Student" and student.school_id == 1,
      preload: [:class_groups],
      preload: [parent_students: ^from(ps in ParentStudent, select: ps.parent_id)]

    render conn, "index.json", students: Repo.all query # <- Argument error, that's all
  end

  def show(conn, params) do
    student = Repo.get(Student, params["id"])
      |> Repo.preload [:class_groups, :parent_students]
    render conn, "show.json", student: student
  end
end

@cibernox
Copy link
Author

** (exit) an exception was raised:
** (ArgumentError) argument error
(ecto) lib/ecto/repo/preloader.ex:49: Ecto.Repo.Preloader.do_preload/4
(ecto) lib/ecto/repo/preloader.ex:19: Ecto.Repo.Preloader.query/5
(ecto) lib/ecto/repo/queryable.ex:99: Ecto.Repo.Queryable.execute/5
(ecto) lib/ecto/repo/queryable.ex:15: Ecto.Repo.Queryable.all/4
(smhw_phoenix) web/controllers/student_controller.ex:19: SmhwPhoenix.StudentController.index/2
(smhw_phoenix) web/controllers/student_controller.ex:1: SmhwPhoenix.StudentController.phoenix_controller_pipeline/2
(smhw_phoenix) lib/phoenix/router.ex:265: SmhwPhoenix.Router.dispatch/2
(smhw_phoenix) web/router.ex:1: SmhwPhoenix.Router.do_call/2
(smhw_phoenix) lib/smhw_phoenix/endpoint.ex:1: SmhwPhoenix.Endpoint.phoenix_pipeline/1
(smhw_phoenix) lib/plug/debugger.ex:90: SmhwPhoenix.Endpoint."call (overridable 3)"/2
(smhw_phoenix) lib/phoenix/endpoint/render_errors.ex:34: SmhwPhoenix.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

@cibernox
Copy link
Author

defmodule SmhwPhoenix.Student do
  use Ecto.Model

  schema "users" do
    field :avatar,                  :string
    field :forename,                :string
    field :school_id,               :integer
    field :surname,                 :string
    field :title,                   :string
    field :type,                    :string
    field :is_mobile_beta,          :boolean,  default: false
    field :student_friday_summary,  :boolean,  default: false
    field :student_overdue_summary, :boolean,  default: false
    field :student_marked_homework, :boolean,  default: false
    field :push_notification,       :boolean,  default: false
    field :sims_id,                 :string
    timestamps([{:inserted_at, :created_at}])

    has_many :class_group_assignments, SmhwPhoenix.ClassGroupAssignment
    has_many :parent_students, SmhwPhoenix.ParentStudent

    has_many :class_groups, through: [:class_group_assignments, :class_group]
    has_many :parents, through: [:parent_students, :parent]
  end
end

defmodule SmhwPhoenix.ParentStudent do
  use Ecto.Model

  schema "parent_students" do
    belongs_to :parent, SmhwPhoenix.Parent
    belongs_to :student, SmhwPhoenix.Student
  end
end

defmodule SmhwPhoenix.ClassGroupAssignment do
  use Ecto.Model

  schema "class_group_assignments" do
    belongs_to :class_group, SmhwPhoenix.ClassGroup
    belongs_to :student, SmhwPhoenix.Student
  end
end

defmodule SmhwPhoenix.ClassGroup do
  use Ecto.Model

  schema "class_groups" do
    field :name,                  :string
    field :class_year,            :string
    field :is_registration_group, :boolean, default: false
    field :imported_via_sims,     :boolean, default: false
    timestamps([{:inserted_at, :created_at}])

    # Fields not yet ported
    # t.integer  "school_id",             limit: 4
    # t.integer  "teacher_id",            limit: 4
    # t.integer  "academic_year_id",      limit: 4
    # t.boolean  "setup_finished",        limit: 1,   default: false
    # t.boolean  "letters_printed",       limit: 1,   default: false
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment