我假设以下设置:
class User < AR
has_many :memberships
has_many :teams, through: :memberships
end
class Membership < AR
belongs_to :user
belongs_to :team
end
class Team < AR
has_many :memberships
has_many :teams, through: :memberships
end
那么这应该工作:
sql = <<QUERY
SELECT users.* FROM users WHERE users.privacy_setting = 'public'
UNION
SELECT users.* FROM users JOIN memberships ON users.id = memberships.user_id
WHERE memberships.team_id IN (#{current_user.team_ids})
AND users.privacy_setting = 'protected'
QUERY
# use the paginated find_by_sql method (provided by will_paginate)
User.paginate_by_sql(sql, page: params[:page], per_page: 50)
当然,列名等取决于您的设置...
PS:不需要AREL,我想...