为什么这些named_scopes会导致重复的INNER JOIN?
我有一个模型,用于使用 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_scope
s, 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_scope
s 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_scope
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将 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.