Rails habtm and finding record with no association
Asked Answered
M

3

13

I have 2 models:

class User < ActiveRecord::Base
    has_and_belongs_to_many :groups
end

class Group < ActiveRecord::Base
    has_and_belongs_to_many :users
end

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)

Morra answered 11/8, 2011 at 20:11 Comment(0)
S
18

Your implicit join table would have been named groups_users based on naming conventions. Confirm it once in your db. Assuming it is:

In newer Rails version:

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

For older Rails versions:

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.*"
}
Saleh answered 11/8, 2011 at 20:20 Comment(2)
Would you need DISTINCT in this case where there would be no join relationship for the returned results, and thus no repetition of users?Stansberry
DISTINCT is not needed. I added the new syntax required for Rails 4 and above (I think).Insurgency
B
3

If you convert from HABTM to has_many through (more flexible) association, then you can use something like this:

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
Brackely answered 20/7, 2015 at 19:33 Comment(0)
H
2

In Rails >= 5, there is left_outer_joins, combined with the new(ish) .where() syntax, makes the scope a bit more readable:

class User < ActiveRecord::Base
  has_and_belongs_to_many :groups

  scope :not_in_any_group, -> {
    left_outer_joins(:groups)
    .where(groups_users: { user_id: nil })
  }
end

class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
end

Hackett answered 24/3, 2021 at 9:47 Comment(1)
Unlike the accepted answer this performs two joins. users to groups_users to groups.Bannasch

© 2022 - 2024 — McMap. All rights reserved.