0

awesome_nested_setプラグインを使用して階層組織の権限を追跡するために使用しているモデルがあります。named_scope2 つの がチェーンされていると、重複が発生するという問題が発生してい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

2 つ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_scopejoin する必要がありmembershipsますが、重複せずにそれを行うことができるべきではありませんか? それらが連鎖すると、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
4

1 に答える 1

1

for 名前付きスコープの結合ステートメントを に変更してみてください:joins => {:memberships => :groups}。has_many through 関係が原因であると思われます。

于 2010-07-09T19:28:50.767 に答える