limit fails when used with belongsToMany+hasMany association - even with separate: true
See original GitHub issueHi,
I’m not quite sure whether it is really a bug or I just did something wrong but I have some issues getting data from a belongsToMany
association which itself has another hasMany
association which I want to limit to only 1 result in one certain case.
I have a Product
model which has many ProductImages
and belongs to many ProductCategories
. When I try to get all products of a certain category with only the first product image included, I get an error Invalid scope ‘overview’ called - overview is the scope I use to get only the first product image as preview.
If I get the product directly via Product.scope('overview').findAll()
everything works fine. I’ve already tried separate: true
but with no success.
I created a fully executable gist so you can have a look: https://gist.github.com/manuelbieh/b0ec0f41614cf9924b14
(MySQL Version here: https://gist.github.com/manuelbieh/a7a0aa14b3ef53e0bb78)
Any idea what I’ve done wrong and/or how it can be fixed? Or is it really a bug?
Maybe a short explanation of what I want to do to get some context: I have a page with a category page where a user can see all products of that particular category. For each product in that list I want to show the product’s first image as preview, so I really need only the first image. How can I realize that?
Issue Analytics
- State:
- Created 8 years ago
- Comments:13 (7 by maintainers)
I might have found another bug related to the scoping bug: adding
attributes
to the scope causes another error:Either
limit
orattributes
can be used. Iflimit
is set,attributes
causes the error mentioned above. Leavinglimit
out,attributes
works fine.👍