Reputation: 4223
I have these associations:
Room hasMany Key
User hasMany Key
Now I want to find all rooms a specific user has a key for.
At the moment I get all keys that have a specific user_id
.
Then I get all rooms and filter out the ones that don't have one of those found keys. Which seems quite dumb, since all of that could be done with one SQL command.
I could probably pluck out the room_id
of every key and use them in the query for the rooms, but those are still two requests.
Upvotes: 0
Views: 282
Reputation: 2694
You should be able to use prefetching/includes to solve this.
You can use something like:
Room.findAll({
include: [
{model: Key, where: {user_id: 1}}
]
});
Or
Room.findAll({
include: [
{model: Key, include: [
{model: User, where: {id: 1}}
]}
]
});
Upvotes: 2