warronbebster
Multiple has_many associations, different name, same struct
I’ve got a Schema “Details” and a Schema “Buildables”. Inside of Details, I want it to have multiple associations (shown below) but they’re all different iterations of the same schema, Buildables. Is this possible or do I have to make a unique schema for each of these associations?
schema "details" do
has_many :single_family_homes, Buildable
has_many :multi_family_homes, Buildable
has_many :apartments, Buildable
has_many :micro_apartments, Buildable
has_many :high_rises, Buildable
schema "buildable" do
field :enabled, :boolean
field :reason, {:array, :string}
field :upgrades, :map
belongs_to :details, MayorGame.City.Details
Most Liked
al2o3cr
The code shown in your post should certainly work, but it doesn’t include any distinction between the different kinds of Buildable. Assuming there is a type on Buildable as @zwippie did, you could filter the associations with where:
has_many :single_family_homes, Buildable, where: [type: "single_family_home"]
etc
See also the note under “Important!” at the bottom of that section.
1
Popular in Questions
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
can someone please explain to me how Enum.reduce works with maps
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New







