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
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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, eachBloghas manyPostsand each post has manyComments.Let's say I want to load all
Blogs, but for eachBlogonly the featuredPostreferenced by theblogs.featured_post_idand for each suchPostonly the latestComment(created_at DESC).I'd start to write something like this:
This means the
PostLoad needs to refer to theBlogtable and theCommentLoad needs to refer to thePosttable.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
CommentperPost, 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
BlogtoCommentelsewhere 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 eachPostid.Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions