excuse my ignorance, but I am stumped on what I expect is a simple and common thing :<
In Phoenix 1.4-dev I am trying to access the value of a key in a struct
when I do get_by! I can access key simply with struct.key
iex(62)> website = Repo.get_by!(Website, domain: "countrywebsite.com")
%WwwWebsiteCom.Web.Websites.Website{
__meta__: #Ecto.Schema.Metadata<:loaded, "websites_websites">,
blogs: #Ecto.Association.NotLoaded<association :blogs is not loaded>,
description: "country website domain",
domain: "countrywebsite.com",
id: 2,
inserted_at: ~N[2018-09-22 06:05:58.447615],
title: "Country Website",
updated_at: ~N[2018-09-22 06:05:58.447624],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 1
}
iex(63)> website.id
2
but when I run a similar where from, the struct is inside a list and its not obvious how to access the key
iex(64)> website = Repo.all(from d in Website, where: like(d.domain, "countrywebsite.com"))
[
%WwwWebsiteCom.Web.Websites.Website{
__meta__: #Ecto.Schema.Metadata<:loaded, "websites_websites">,
blogs: #Ecto.Association.NotLoaded<association :blogs is not loaded>,
description: "Country Website",
domain: "countrywebsite.com",
id: 2,
inserted_at: ~N[2018-09-22 06:05:58.447615],
title: "Country Website",
updated_at: ~N[2018-09-22 06:05:58.447624],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 1
}
]
iex(65)> website.id
** (ArgumentError) you attempted to apply :id on [%WwwCountryCom.Web.Websites.Website
is there a way to flatten or strip the list? convert the list or extract the struct from the list?