Hello all, long time listener first time caller.
I have been using Elixir/Phoenix for last few months and really got hooked. Created few schemaless apps, dealing with json reading/writing. Now in my first attempt to deal with Ecto, there are still few issues I canβt wrap my mind around. I understand the concept of preloading and definitely like the idea.
My issue is that I have Post schema, which has_many Comment. I want to list all posts on one page, but with two special requests.
First: I want to limit preloading to 3 latest comments per post (not all of them, as there might be hundreds potentially).
Second: I want to preload only title column from the comment, and avoid βtextβ column, as the text might potentially contain too much content.
My final result would be:
Post 1 βββ¬ββ Comment 1 Title
βββ Comment 2 Title
βββ Comment 3 Title
Post 2 βββ¬ββ Comment 1 Title
βββ Comment 2 Title
βββ Comment 3 Title
Post 3 βββ¬ββ Comment 1 Title
βββ Comment 2 Title
βββ Comment 3 Title
...(etc)
Whatever combination I have tried, I fail to limit number of nested comment per post (my limits always limit total number of comments, rather than on per-post basis).
Also my selects fail to load title only from the comments. If anyone with experience has any inputs, it would be more than appreciated. I am still learning and generally donβt have much experience in relational databases (thatβs on my bucket list). Many thanks!
PS it is already suggested above, but for more clarification, hereβs my model:
schema "posts" do
field :title, :string
field :slug, :string
field :active, :boolean, default: true
has_many :comments, App.Comment
timestamps()
end
schema "comments" do
field :title, :string
field :body, :string
field :email, :string
field :meta, :map
field :active, :boolean, default: false
belongs_to :post, App.Post
timestamps()
end
EDIT: to be even more specific, I was wondering if it is possible to have nested limits, in same manner as nested preloads:
query = from Post, preload: [:file, :image, {:comments, [:user, :icon]}], limit: [10, {:comments: 3}]
That preload will preload nested user and icon column in comments, but limit obviously does not work for nested records.