Referencing other tables in a qm.Load() #895
yourtallness
started this conversation in
General
Replies: 1 comment
-
This is an issue with the current version of eager loading and unfortunately there's no way around it at this current moment. Your only option is to use a raw query for things that get too complicated. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I'm trying to eager load a has-many-through association but find it difficult to apply conditions that refer to other tables.
I've seen the examples of
Limit(1)
(on Youtube) orWhere("some_column = false")
(in the repo README), but these are not dependent on other tables.e.g. Assume a site has many
Blogs
, eachBlog
has manyPosts
and each post has manyComments
.Let's say I want to load all
Blogs
, but for eachBlog
only the featuredPost
referenced by theblogs.featured_post_id
and for each suchPost
only the latestComment
(created_at DESC
).I'd start to write something like this:
This means the
Post
Load needs to refer to theBlog
table and theComment
Load needs to refer to thePost
table.For the featured criterion, I'd need to keep only the Posts where
blogs.featured_post_id = posts.id
.For the date criterion, in SQL I'd use a self-join with a sub-select like described here to get one latest
Comment
perPost
, by ordering bycreated_at
.As far as I can tell, since each eager load is done in a subsequent query, references to tables of the original query are not in scope.
How would I tackle the above issue? I would like to be able to use the following way of getting from
Blog
toComment
elsewhere in my mode (e.g. in my serializer).Note:
qm.Loads("Comments", OrderBy("created_at DESC"), qm.Limit(1))
does not solve the date case because it will apply the limit to the entire query, not scoped to eachPost
id.Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions