Take the 2-minute tour ×  I have 2 models:  class User <
ActiveRecord::Base     has_and_belongs_to_many :groups  class
Group < ActiveRecord::Base     has_and_belongs_to_many :users  I
want to make a scope (that's important - for efficiency and for
ability to chain scopes) that returns Users that doesn't belong
to ANY Groups. After many tries, I failed in doing a method
instead of scope, which makes collect on User.all which is ugly
and.. not right.  Any help?  And maybe for 2nd question: I
managed to make a scope that returns Users who belongs to any of
given groups (given as an array of id's).  scope :in_groups,
lambda { |g|           :joins      => :groups,
:conditions => {:groups => {:id => g}},           :select     =>
"DISTINCT `users`.*" # kill duplicates  Can it be
better/prettier? (Using Rails 3.0.9)  share|improve this
question add comment  1 Answer  up vote 8 down vote accepted
Your implicit join table would have been named groups_users
based on naming conventions. Confirm it once in your