Hello, I’ve been banging my head against this problem for a couple of hours and would appreciate any help.
The problem I’m running into is in the system there are multiple lockers associated to a locker system and multiple cubbies associated to a locker.
What I’m trying to is tie the list of cubbies directly with the locker system.
I have the following schemas:
@primary_key {:id, :string, autogenerate: false}
@derive {Jason.Encoder, only: [:timer, :serialnumber]}
schema "lockersystem" do
field :timer, :integer
field :serialnumber, :string
field :location, :string
has_many :lockers, Locker, foreign_key: :lockersystem
has_one :mastercode, Sfecfg, foreign_key: :name, references: :location,
where: [cfgtype: "LOC", type: "LOCKERLOCATIONMASTERCODE"]
end
@primary_key {:id, :string, autogenerate: false}
@foreign_key_type :string
@derive {Jason.Encoder, only: [:lockertype]}
schema "locker" do
field :lockertype, :string
belongs_to :locker_system, LockerSystem, foreign_key: :lockersystem
has_many :cubbies, Cubby, foreign_key: :locker
end
@primary_key {:id, :string, autogenerate: false}
@foreign_key_type :string
@derive {Jason.Encoder, only: [:id, :address]}
schema "cubby" do
field :address, :string
belongs_to :lockers, Locker, foreign_key: :locker
end
This is the current code I’m running to get the results.
LockerSystem
|> Repo.all
|> Repo.preload([:mastercode, lockers: :cubbies])
|> Enum.map(fn %{timer: timer, serialnumber: serialnumber, mastercode: mastercode, lockers: lockers} ->
%{timout: timer, serial_number: serialnumber, master_code: mastercode.value, lockers: lockers, last_updated: DateTime.utc_now()}
end)
It seems to be returning everything I need. I’m just not sure how to change the query to get the format I need it in.