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) Any idea? Thanks for help.. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8-utqmuuwNIJ. For more options, visit https://groups.google.com/groups/opt_out.
On Fri, Sep 21, 2012 at 2:48 AM, Werner <webagentur.laude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 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 1Stop thinking in terms of tables and think about your models, e.g. skills_you_want_to_display = Skill.all - user.skills -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Am 21.09.2012 um 17:40 schrieb Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> On Fri, Sep 21, 2012 at 2:48 AM, Werner <webagentur.laude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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. > > skills_you_want_to_display = Skill.all - user.skillscan you give me a practical sample..thanks> > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassan > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >Werner Laude webagentur.laude-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Fri, Sep 21, 2012 at 8:46 AM, Werner Laude <webagentur.laude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> can you give me a practical sample..thanksI just did :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Am 21.09.2012 um 17:47 schrieb Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> On Fri, Sep 21, 2012 at 8:46 AM, Werner Laude > <webagentur.laude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> can you give me a practical sample..thanks > > I just did :-)This idea Skill.all - user.skills is totally new to me.. I will try tonight..ned some air now Thanks so far> > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassan > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >Werner Laude webagentur.laude-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Fri, Sep 21, 2012 at 8:54 AM, Werner Laude <webagentur.laude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> This idea > > Skill.all - user.skills > > is totally new to me..Time to open up irb or a rails console and play around, e.g. 1.9.3p194 :014 > [1,2,3,4,5] - [1,2,3] => [4, 5] Have fun! -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
I won''t generally go with that approach: - Skill.all can get big - 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 Schroeder < hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Sep 21, 2012 at 8:54 AM, Werner Laude > <webagentur.laude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > This idea > > > > Skill.all - user.skills > > > > is totally new to me.. > > Time to open up irb or a rails console and play around, e.g. > > 1.9.3p194 :014 > [1,2,3,4,5] - [1,2,3] > => [4, 5] > > Have fun! > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassan > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Fri, Sep 21, 2012 at 12:17 PM, Mirri Kim <mirri.kim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I won''t generally go with that approach: > > - Skill.all can get big > - You''re doing 2 queries by default plus the array subtractionI don''t think the two-queries part can be avoided. If Skill.all is big, maybe something like: Skill.where("skills.id NOT IN ?", user.skills.map(&:id)) Whatcha think? -Dave -- Dave Aronson, Available Secret-Cleared Ruby/Rails Freelancer (VA/DC/Remote); see www.DaveAronson.com, www.Codosaur.us, and www.Dare2XL.com for more info. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.