如何覆盖 has_many :through 关联中的默认范围?
How can I override a default scope in a has_many :through association?
在 has_many :through 关联中不会(以有用的方式)持久覆盖默认范围。
以下是模型中的相关内容:
class User
has_one :invitation
default_scope where(registered: true)
end
class Invitation
belongs_to :user, conditions: { registered: [true, false] }
belongs_to :program
end
class Program
has_many :invitations
has_many :users, through: :invitations
end
通过邀请查找用户按预期工作。
Invitation.joins(:user)
生成这个:
SELECT `invitations`.* FROM `invitations` INNER JOIN `users` ON `users`.`id` = `invitations`.`user_id` AND `users`.`registered` IN (1, 0)
鉴于此,我假设 Program.last.users 应该找到所有相关用户,无论注册是真是假。相反,我得到这个:
SELECT `users`.* FROM `users` INNER JOIN `invitations` ON `users`.`id` = `invitations`.`user_id` WHERE `users`.`registered` = 1 AND `invitations`.`program_id` = 52 AND (`users`.`registered` IN (1, 0))
这是我期望的SQL。我如何创建一个给我这个的协会?
SELECT `users`.* FROM `users` INNER JOIN `invitations` ON `users`.`id` = `invitations`.`user_id` WHERE `users`.`registered` IN (1, 0) AND `invitations`.`program_id` = 52
更多上下文:我使用的是 rails 3.1.3,升级 rails 不是一个可行的选择,在此功能需要退出之前。摆脱 default_scope 也不是我能做的事。
class Program
# override association with a method
def users
Program.joins(:invitation).joins(:users).where('users.registered = true')....ETC
end
end
@program = Program.last
@user = @program.users.unscoped.all
我能够使用 has_and_belongs_to_many 关联解决此问题。在此关联上设置的条件覆盖用户的默认范围。
class Program
has_and_belongs_to_many :users,
join_table: :invitations,
conditions: {registered: [true, false]}
end
program.users
产生这个 SQL:
SELECT `users`.*
FROM `users`
INNER JOIN `invitations`
ON `users`.`id` = `invitations`.`user_id`
WHERE `invitations`.`program_id` = 111
AND `users`.`registered` IN (1, 0)
在 has_many :through 关联中不会(以有用的方式)持久覆盖默认范围。
以下是模型中的相关内容:
class User
has_one :invitation
default_scope where(registered: true)
end
class Invitation
belongs_to :user, conditions: { registered: [true, false] }
belongs_to :program
end
class Program
has_many :invitations
has_many :users, through: :invitations
end
通过邀请查找用户按预期工作。
Invitation.joins(:user)
生成这个:
SELECT `invitations`.* FROM `invitations` INNER JOIN `users` ON `users`.`id` = `invitations`.`user_id` AND `users`.`registered` IN (1, 0)
鉴于此,我假设 Program.last.users 应该找到所有相关用户,无论注册是真是假。相反,我得到这个:
SELECT `users`.* FROM `users` INNER JOIN `invitations` ON `users`.`id` = `invitations`.`user_id` WHERE `users`.`registered` = 1 AND `invitations`.`program_id` = 52 AND (`users`.`registered` IN (1, 0))
这是我期望的SQL。我如何创建一个给我这个的协会?
SELECT `users`.* FROM `users` INNER JOIN `invitations` ON `users`.`id` = `invitations`.`user_id` WHERE `users`.`registered` IN (1, 0) AND `invitations`.`program_id` = 52
更多上下文:我使用的是 rails 3.1.3,升级 rails 不是一个可行的选择,在此功能需要退出之前。摆脱 default_scope 也不是我能做的事。
class Program
# override association with a method
def users
Program.joins(:invitation).joins(:users).where('users.registered = true')....ETC
end
end
@program = Program.last
@user = @program.users.unscoped.all
我能够使用 has_and_belongs_to_many 关联解决此问题。在此关联上设置的条件覆盖用户的默认范围。
class Program
has_and_belongs_to_many :users,
join_table: :invitations,
conditions: {registered: [true, false]}
end
program.users
产生这个 SQL:
SELECT `users`.*
FROM `users`
INNER JOIN `invitations`
ON `users`.`id` = `invitations`.`user_id`
WHERE `invitations`.`program_id` = 111
AND `users`.`registered` IN (1, 0)