我有一个具有默认范围的模型类,像这样
class Avatar
self.table_name = 'attachments'
belongs_to :user
default_scope -> { where(type: 'avatar') }
end
class User
has_one :avatar
end
我希望User.first.avatar
返回用户创建的第一个附件类型为头像。相反,我得到了用户的第一个附件(无论类型如何)。
我必须手动指定类型才能使其工作:
has_one :avatar, -> { where(type: 'avatar') }
为什么关联不尊重 default_scope?我应该添加Avatar.all
返回预期结果(仅在类型为“头像”的情况下)。