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

  • Home
  • SEARCH
  • 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 906539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:24:15+00:00 2026-05-15T16:24:15+00:00

I have a Model which I am using to track permissions in a hierarchical

  • 0

I have a Model which I am using to track permissions in a hierarchical organization using the awesome_nested_set plugin. I’m running into a problem where two named_scopes, when chained together, are creating a duplication INNER JOIN.

class Group < ActiveRecord::Base
  acts_as_nested_set
  has_many :memberships
  has_many :accounts, :through => :memberships
  has_many :authorizations
  has_many :users, :through => :authorizations
end

The two named_scopes are located in the Account model, and are used to filter accounts by a user and by a group:

named_scope :in_group, lambda { |id|
  group_ids = Group.find(id).self_and_descendants.collect(&:id)
  { :joins => :memberships, :conditions => ["memberships.group_id in (?)", group_ids] }
}

named_scope :for, lambda { |user|
  groups = user.authorizations.groups.collect(&:id) unless user.admin?
  user.admin ? {} : { :joins => :groups, :conditions => ["groups.id IN (?)", groups] }
}

Both of these named_scopes need to join memberships, but shouldn’t they be able to do that without duplication? When they are chained together, mysql blows up with the following error:

Mysql::Error: Not unique table/alias: 'memberships': SELECT `accounts`.* FROM `accounts` INNER JOIN `memberships` ON accounts.id = memberships.account_id INNER JOIN `memberships` ON `accounts`.`id` = `memberships`.`account_id` INNER JOIN `groups` ON `groups`.`id` = `memberships`.`group_id` WHERE ((memberships.group_id in (54,94)) AND (groups.id IN (54,94)))  ORDER BY business_name, location_name LIMIT 0, 75
  • 1 1 Answer
  • 1 View
  • 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-05-15T16:24:16+00:00Added an answer on May 15, 2026 at 4:24 pm

    Try changing the join statement in the for named scope to :joins => {:memberships => :groups}. I suspect that the has_many through relationship might be causing it.

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

Sidebar

Related Questions

I'm using ASP.NET MVC and have a model which has an image (byte array)
I have a form which binds a model and a file upload using the
I have the following model class Plugin(models.Model): name = models.CharField(max_length=50) # more fields which
I have been working on a Spotify app which displays a track listing using
I have model Foo which has field bar. The bar field should be unique,
I have django model which consist of parent child relationship filed I want to
I have a model which defines a property with either markdown or html content.
I have a model which has a certain behaviour implemented. class X { ....
I have a model Task which will HABTM many TaskTargets. When it comes to
I have an Entry model which has many Tag s. Tag s are added

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.