Scope for Self-joining HABTM Association
问题 With Rails 3.1.3 on Ruby 1.9.2 , I have: class User < ActiveRecord::Base has_and_belongs_to_many :states end class State < ActiveRecord::Base has_and_belongs_to_many :users end A User is associated with many State s, and a State with many User s. Given a User , I want to find all other User s who are in any of her states. I tried: class User < ActiveRecord::Base scope :in_state, lambda { |states| joins(:states).where(:states => states) } end User.in_state(current_user.states).all With this