【发布时间】:2013-12-12 03:41:25
【问题描述】:
我正在尝试对 has_and_belongs_to_many 关系实施唯一约束,如下所示:
class User
has_and_belongs_to_many :foos, uniq: true
end
因为当我调用user.foos 时我只想要唯一的foos,所以我添加了uniq 选项。自从升级到 Rails 4 后,我开始收到以下警告:
弃用警告:您的以下选项 User.has_and_belongs_to_many :foos 声明已弃用: :uniq。 请改用范围块。例如:
has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'应该改写如下:
has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
我尝试了许多不同的组合,并通读了源代码,但不知道如何编写唯一约束以消除警告?
【问题讨论】:
-
也许this 可以提供帮助
标签: ruby-on-rails activerecord ruby-on-rails-4