Hallo.
I have a user and a skill table joined in a SkillUser table. (has many
through)
Now I try to get all Skills listed which are not already in the join
table
for user 1
In other words: user 1 has entries in the SkillUser table for skill 1
and
2. And I want to get skill 3…to …in the result, but not 1 or 2.
I have tried:
skills = Skill.sort.includes(:skill_users).where(‘skill_users.user_id
NOT
IN (?)’, @user.id)
But then I only get all the skills listed for all other users already
listed in the SkillUser table.
Somehow I thing about a filter like
skill = Skill.sort
skill.filter(substract skills for user x which are already in SkillUser)
I have a user and a skill table joined in a SkillUser table. (has many
through)
Now I try to get all Skills listed which are not already in the join table
for user 1
Stop thinking in terms of tables and think about your models, e.g.
On Fri, Sep 21, 2012 at 2:48 AM, Werner [email protected] wrote:
Hi Hassan…
I have a user and a skill table joined in a SkillUser table. (has many
through)
Now I try to get all Skills listed which are not already in the join table
for user 1
Stop thinking in terms of tables and think about your models, e.g.
You’re doing 2 queries by default plus the array subtraction
It’s better to just use a scope or a class method on Skill or some
service
class in my opinion but for brevity nothing tops that. The results would
(should) be cached anyway.
On Fri, Sep 21, 2012 at 11:57 PM, Hassan S. <
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.