Rails 4在使用has_many :through时,使用:uniq => true会出现弃用警告。例如:
产生如下警告:
has_many :donors, :through => :donations, :uniq => true
产生如下警告:
DEPRECATION WARNING: The following options in your Goal.has_many :donors declaration are deprecated: :uniq. Please use a scope block instead. For example, the following:
has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'
should be rewritten as the following:
has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
如何正确重写上述 has_many 声明?
distinct
而不是uniq
。更多详情请参阅此答案。 - Nic Nilov