![]() ![]() It will basically run a query to find all images with imageable_id equal to the user's ID and with imageable_type "user" This association will allow us to access all images the user has simply by calling Repo.preload(:images). Has many :images, Image, foreignkey: :imageable id, where: We are going to add a has_many association to our schema. The users schema should currently look like this: Here we have added a virtual :imageable field which later on we will populate with either a user or a product record. In Rails the go-to approach would be to define a polymorphic association like this: class Image cast(attrs, ) Say we have a User model, an Image model, and a Product model.Īn Image can belong to a User or to a Product. The purpose of this guide is to clear that up. If you've already gone through the Ecto documentation you've probably found out that copying this functionality into Еlixir is not recommended and is not the Еlixir way, yet no one actually describes how you could go about it if you want to replicate it. We recently had to migrate a Rails functionality to Elixir without making any changes to our database, so in this article we'll cover how you can achieve that. Sorry, was a little clueless statement from my side.If you're coming from Ruby on Rails, you're probably familiar with the polymorphic associations between resources and you probably miss them in Elixir. A function that creates an Ecto.Multi can be directly fed to ansaction. I am looking for the good practices in the area.ĮDIT #1: /CC #2: Nevermind the ansaction comment. Maybe Ecto.Multi?Īpologies for requesting some hand-holding. What would you recommend to me? I can’t use ansaction – there are several separate functions that operate on the order changeset. Preloading the same association several times in short-lived orders – like in tests – feels like a huge anti-pattern though. I have several functions that preload various associations as the order changeset is going through different states (constructing with a few fields, adding shipping and billing addresses, adding line items, setting various delivery fields, sending transactional emails and storing their sending timestamp etc.) and I deliberately chose not to use Ecto.assoc_loaded? because I am afraid of stale data – some of the orders are carts and might live for hours or days, for example. Shall I gather from your comments that replacing the data field of the Changeset after it has been created is a really bad idea? I think so and I am looking for a confirmation. Ecto: How to update and insert association in one update using put_assoc – this is fine and very easy to do but I’d yet again have to preload line items before being able to add a line item so the same problem & Thank you guys.Loading association data in Ecto changeset (I posted at the bottom).I have a load of chain-able functions with specs like these: order_add_something(%Changeset? Or is this actually advised? I always assume code like this is unsafe so I need your advice.Īnd finally, a quick research gave me these: The second point is becoming a major pain. Using Changeset.get_field would return the association-not-loaded value. Being able to add associations which are dependent on other associations, all inside a changeset (strongly related to point 1).Being able to accumulate arbitrary amount of changes before even a single database operation is made.Lately I am reworking a module with the following goals in mind: How do we add preloaded associations to a changeset and reuse them in several functions that need them in order to accumulate further changes in the changeset? All before persisting to the DB? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |