Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:22:08+00:00 2026-06-11T02:22:08+00:00

With Rails 3.1.3 on Ruby 1.9.2 , I have: class User < ActiveRecord::Base has_and_belongs_to_many

  • 0

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 States, and a State with many Users. Given a User, I want to find all other Users 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 error (formatted for readability):

Mysql2::Error: Not unique table/alias: 'states_users': 
SELECT COUNT(*) 
FROM `users` 
  INNER JOIN `states_users` ON `states_users`.`user_id` = `users`.`id` 
  INNER JOIN `states` ON `states`.`id` = `states_users`.`state_id` 
  LEFT OUTER JOIN states_users on users.id = states_users.user_id AND states_users.state_id IS NULL 
WHERE `states`.`id` IN (8)

I realize I need some way to alias one of the states_users references, but I’m at a loss for figuring out how to do this with scope.

Here’s the SQL I would write:

SELECT u2.*
FROM users u
  INNER JOIN states_users su ON su.user_id = u.id
  INNER JOIN states s ON s.id = su.state_id
  INNER JOIN states_users su2 ON s.id = su2.state_id
  INNER JOIN users u2 ON u2.id = su2.user_id
WHERE u.id = 771

Thoughts?

Thanks.

UPDATE 12/14/2011 @ 10:48 AM:

Here’s what’s working:

scope :in_states_of, lambda { |user|
  joins('INNER JOIN states_users su ON su.user_id = users.id ' +
        'INNER JOIN states s ON s.id = su.state_id '           +
        'INNER JOIN states_users su2 ON s.id = su2.state_id '  +
        'INNER JOIN users u ON u.id = su2.user_id').
  where("u.id = ?", user.id) }

User.in_states_of(current_user)

It’s not particularly elegant, but it’s working. You have to alias the users reference for the “incoming” User so you can chain this scope with others, such as:

scope :active, where(active => true)

User.in_states_of(current_user).active

Any suggestions for improvement would be most welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:22:10+00:00Added an answer on June 11, 2026 at 2:22 am

    Here’s what’s working:

    scope :in_states_of, lambda { |user|
      joins('INNER JOIN states_users su ON su.user_id = users.id ' +
            'INNER JOIN states s ON s.id = su.state_id '           +
            'INNER JOIN states_users su2 ON s.id = su2.state_id '  +
            'INNER JOIN users u ON u.id = su2.user_id').
      where("u.id = ?", user.id) }
    
    User.in_states_of(current_user)
    

    It’s not particularly elegant, but it’s working. You have to alias the users reference for the “incoming” User so you can chain this scope with others, such as:

    scope :active, where(active => true)
    
    User.in_states_of(current_user).active
    

    Any suggestions for improvement would be most welcome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I have now: class User < ActiveRecord::Base has_many :people end ... and... class
Rails 3.0.3 and Ruby 1.9.2 I have the following class class item < ActiveRecord::Base
I'm using Rails 3. I have 3 models: class Deal < ActiveRecord::Base has_many :wishes,
I have the following code for my user model: class User < ActiveRecord::Base has_secure_password
My code: class User < ActiveRecord::Base belongs_to :university end class University < ActiveRecord::Base has_many
I have a ruby (1.9.3) on rails (3.1) app where: app/models/list.rb class List <
I'm using ruby 1.9.2 and rails 3.2.2. I have a 'domain' model (domain.rb): class
I have a simple model setup in my Ruby on Rails app. (User {name,
I'm new to ruby on rails. So i think i have association problems. Given
I am writing ruby on rails application, which will have 2 different users types

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.