为什么这些named_scopes会导致重复的INNER JOIN?

发布于 2024-09-08 12:27:55 字数 1432 浏览 1 评论 0原文

我有一个模型,用于使用 awesome_nested_set 插件跟踪分层组织中的权限。我遇到了一个问题,两个 named_scope 链接在一起时会创建重复的 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

这两个 named_scope 位于 Account 模型中,用于按用户和组筛选帐户:

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] }
}

这两个 named_scope需要加入会员资格,但他们不应该能够在不重复的情况下做到这一点吗?当它们链接在一起时,mysql 会崩溃并出现以下错误:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一世旳自豪 2024-09-15 12:27:55

尝试将 for 命名范围中的 join 语句更改为 :joins => {:会员=> :组}。我怀疑 has_many 通过关系可能会导致它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文