9/2/2023 0 Comments Elixir ecto subquery![]() ![]() The table which they get stored within has been created with a migration that resembled the code sample below.ĭefmodule Promotions.UserAction do use Ecto.Schema alias alias alias alias schema "user_actions" do field :action_id, :string field :action_type, Ecto.Enum, values : field :occurred_at, :utc_datetime_usec field :region, Ecto.Enum, values : field :user_id, Ecto.UUID embeds_one :casino_wager_attributes, CasinoWagerAttributes, source : :attributes embeds_one :deposit_attributes, DepositAttributes, source : :attributes embeds_one :registration_attributes, RegistrationAttributes, source : :attributes embeds_one :sportsbook_bet_attributes, SportsbookBetAttributes, source : :attributes timestamps () end endĮach of the various embedded schemas to populate the attributes field is represented in a separate Ecto schema of its own. These actions are used for a variety of purposes within our Promotions service, but in this case, we are exploring looking at the past actions of a given user to see if they fulfill the audience criteria of a given promotional campaign. ![]() These events are received asynchronously from other internal services, via Kafka, and then stored internally in the Promotions database as a generic "user action". The Promotions service depends on the ability to read past events placed by a patron. For example, when creating a new promotion, we can specify that its audience can only include patrons who have previously deposited a specific amount of money, or placed a specific quantity of bets.ĭisclaimer: The specific scenario, and format of the data shown in the examples throughout, is contrived entirely for the purposes of this article. Each promotional campaign allows us to specify an audience of users who will be available to participate in the campaign, by comparing on past actions that a given user has taken against a set of dynamic rules. I'd be very grateful for any insight from some Ecto experts.The Promotions team at theScore is responsible for enabling dynamic marketing campaigns within theScore Bet app. Join: i in subquery(from(i in MyApp.Items, where: i.status_id = 2 )), on: p.id = i.id, I tried to achieve this by using the subquery as a join as follows: Repo.all( I would like to have all of the items associated with that customer if one of the records has a status_id of 2. This Ecto query gives me the customers that have an item with a status_id of 2 but it only returns the qualifying item record. Items: r, items: :provider, items: :type, items: :status Which was a bit of reverse engineering from my current Ecto query: Repo.all( INNER JOIN "items" AS c1 ON c1."customer_id" = c0."id" I have replicated this in SQL as follows: SELECT I also need to preload items, providers, statuses and a types My goal is to have an Ecto query that returns a list of customers that have at least one items record with a status_id of 2 AND any other items that this customers record has. Within items there are some belongsTo relationships: providers items ![]() I currently have two tables that have a hasMany relationship: customers items ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |