Rails habtm并找到没有关联的记录

我有2个型号:

class User < ActiveRecord::Base has_and_belongs_to_many :groups end class Group < ActiveRecord::Base has_and_belongs_to_many :users end 

我想创建一个范围 (这对于效率和链接范围的能力很重要),它返回不属于任何组的用户。 经过多次尝试,我没有做一个方法而不是范围,这使得在User.allcollect丑陋而且……不对。

有帮助吗?

也许对于第二个问题:我设法创建一个范围,返回属于任何给定组的用户(以id的数组给出)。

 scope :in_groups, lambda { |g| { :joins => :groups, :conditions => {:groups => {:id => g}}, :select => "DISTINCT `users`.*" # kill duplicates } } 

可以更好/更漂亮吗? (使用Rails 3.0.9)

您的隐式连接表将根据命名约定命名为groups_users 。 在数据库中确认一次。 假设它是:

在较新的Rails版本中:

 scope :not_in_any_group -> { joins("LEFT JOIN groups_users ON users.id = groups_users.user_id") .where("groups_users.user_id IS NULL") } 

对于较旧的Rails版本:

 scope :not_in_any_group, { :joins => "LEFT JOIN groups_users ON users.id = groups_users.user_id", :conditions => "groups_users.user_id IS NULL", :select => "DISTINCT users.*" } 

如果您从HABTM转换为has_many到(更灵活)关联,那么您可以使用以下内容:

 class Group < ActiveRecord::Base has_many :groups_users, dependent: :destroy has_many :users, through: :groups_users, uniq: true scope :in_groups, -> { includes(:groups_users).where(groups_users: {group_id: nil}) } end class User < ActiveRecord::Base has_many :groups_users, dependent: :destroy has_many :groups, through: :groups_users end class GroupsUser < ActiveRecord::Base belongs_to :group belongs_to :user end