使用has_one关联时,Rails 4不遵守default_scope

时间:2013-11-18 17:47:01

标签: ruby-on-rails associations default-scope

我有一个具有默认范围的模型类,类似这样的

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返回预期的结果(仅在类型为'avatar'的地方)。

0 个答案:

没有答案
相关问题