So I have been digging a little deeper and Ecto is issuing the following query:
*SELECT r0."id", *snip*, o1."offer_id" FROM "rates" AS r0 INNER JOIN "offers_rates" AS o1 ON o1."offer_id" = ANY($1) WHERE (o1."rate_id" = r0."id") ORDER BY o1."offer_id" [[<<163, 3, 242, 94, 74, 2, 77, 110, 159, 78, 123, 64, 97, 42, 5, 33>>]]* This comes up as an empty association in my code. When I do the following query in psql however, I do get the result I expect: *SELECT * FROM rates INNER JOIN offers_rates ON offers_rates.offer_id='a303f25e-4a02-4d6e-9f4e-7b40612a0521' WHERE offers_rates.rate_id = rates.id; * Does this offer some more insight? Op donderdag 26 mei 2016 18:09:47 UTC+2 schreef [email protected]: > > Hi José, > > Thanks for the fast response. That doesn't seem to do it however. If I do > not preload anything, I get an error message saying that the offers are not > preloaded. If I preload the offers, it says the rates are not preloaded, > and if I preload everything, with force set to true, the rates are still > empty. Is there anything else I can try? > > Op donderdag 26 mei 2016 17:07:36 UTC+2 schreef José Valim: >> >> Try: >> >> *offers = Repo.preload(shipment, [offers: :rates], force: true)* >> >> If the association was already loaded, we won't load over it. >> >> *José Valim* >> www.plataformatec.com.br >> Skype: jv.ptec >> Founder and Director of R&D >> >> On Thu, May 26, 2016 at 5:05 PM, <[email protected]> wrote: >> >>> Hello, >>> >>> I have a many-to-many relationship defined as follows: >>> >>> * schema "rates" do* >>> * many_to_many :offers, Offer, join_through: "offers_rates"* >>> * end* >>> >>> >>> * schema "offers" do* >>> * many_to_many :rates, Rate, join_through: "offers_rates"* >>> * end * >>> >>> >>> The offers_rates table is simply a table that has a offer_id column, and >>> a rate_id column. I am saving records as follows: >>> >>> * Enum.map rates, fn (rate) ->* >>> * Offer.changeset(%Offer{rates: [rate]}, %{shipment_id: >>> rate.shipment_id, percentage: 1.05})* >>> * |> Repo.insert!()* >>> * end * >>> >>> >>> For every rate, this is generating an offer. I am association the rates >>> with the offer in the changeset. This seems to work as I can see the join >>> table being filled properly. When I try to fetch them later, the >>> association is empty however: >>> >>> *offers = Repo.preload(shipment, offers: :rates)* >>> >>> >>> * %Offer{rates: []} * >>> >>> >>> Are these the proper ways of association records and fetching them from >>> the database again? Any push in the right direction is greatly appreciated. >>> >>> Thanks! >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "elixir-lang-talk" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/elixir-lang-talk/90e0e96e-a6ca-4111-b4cc-e9d180b99b6b%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/elixir-lang-talk/90e0e96e-a6ca-4111-b4cc-e9d180b99b6b%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/589094f3-2694-4b15-9d39-03d8e4869682%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
