Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

activerecord - Rails habtm and finding record with no association

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.*"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...