hawkyre
As it says in the title, I am trying to fill in a has_many relationship while creating a new record, but everything that I’ve been able to find requires the record to already exist before filling in the has_many.
Here’s the setup that I have:
schema "course" do
has_many(:tiers, CourseTiers)
end
schema "tier" do
has_many(:courses, CourseTiers)
end
schema "course_tiers" do
belongs_to(:course, Course)
belongs_to(:tier, Tier)
field(:order, :integer)
end
The problem comes whenever I try to create a course with the reference to the tiers in the same changeset as the new course. Since neither the course_tiers nor the course exist yet, I haven’t been able to use either build_assoc, cast_assoc, or put_assoc effectively.
Is there any workaround to this that does not involve using multi or performing the insert operation in multiple steps?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
cast_assocshould 100% be able to construct this shape - but your associations are declared strangely.For instance,
has_many(:tiers, CourseTiers)says that you want an association namedtiersonCoursethat hasCourseTiersstructs in it. That’s… not probably what you want.A more-standard set of associations:
Then the changeset for a
Coursecancast_assoc:course_tiers, andCourseTiers.changesetcancast_assoc:tiers.General aside: Ecto doesn’t care much about pluralization, but human readers might. Naming tables with singular forms (
course,tier) and schemas with plural ones (CourseTiers) feels odd IMO.hawkyre
The
CourseTiersnaming thing was just an issue because I did that example without copypasting but it is indeedCourseTierin my code.Alright, I tried renaming the association to
:course_tiersbut it still gives me an error. This is part of the changeset error, the one which shows the invalid changeset (and just one of the elements):I’ll re-post the structure as it is right now (instead of tiers I have tier groups, it was just easier to write it down but it’s the same thing):
Course schema:
Course changeset:
CourseTierGroup schema:
TierGroup schema:
What I pass to the changeset on create:
[%{"tier_group_id" => X, "order" => Y}, ...]On update it works but only if I manually put the
course_idbefore calling the API.Then when that is passed the
course_idproperty ofcourse_tier_groupdoesn’t get filled and it throws an error since the changeset is invalid.al2o3cr
Don’t use
validate_requiredto verify associations; see also the docs:hawkyre
As I understand it, I have to use
cast_associnCourseTierGroupin order for it to work, right? The thing is, if I do so, I would have to pass the whole tier_group object through the API sincecast_assocexpects a whole object to do an update operation in case IDs match. Is that correct? I do not want to perform such update operation, I just want to set the IDs of the respective props in CourseTierGroup.hawkyre
I’ve been trying to change up the format but I still can’t get this to work
dimitarvp
Did you manage to make this work eventually?
hawkyre
Yes, I ended up splitting creation and updating in 2 different flows:
It’s so much more complicated than I expected it to be, but it does the job.