Last active
August 29, 2015 14:27
-
-
Save cibernox/1be2430901d081d46529 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
** (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