在Rails的4 has_and_belongs_to_many关系使用的uniq [英] Using uniq in a has_and_belongs_to_many relationship in Rails 4
问题描述
我想实现像这样一个 has_and_belongs_to_many
关系的唯一约束:
类用户
has_and_belongs_to_many:FOOS,uniq的:真正的
结束
由于我只希望唯一的 FOOS
当我打电话 user.foos
,我加了 uniq的
选项。由于升级到Rails 4,我开始得到以下警告:
DE preCATION警告:在下列选项您 User.has_and_belongs_to_many:FOOS声明是代precated:uniq的。 请使用范围块来代替。例如,以下内容:
的has_many:spam_comments,条件:{垃圾邮件:真},将class_name:注释
应该被改写为:
的has_many:spam_comments, - > {其中,垃圾邮件:真},将class_name:注释
我已经尝试了许多不同的组合,并通过源读取,但无法弄清楚如何写的唯一约束删除警告?
类用户
has_and_belongs_to_many:FOOS, - > {}的uniq
结束
根据<一href="http://edgeguides.rubyonrails.org/association_basics.html#scopes-for-has-and-belongs-to-many">documentation这里
I'm trying to implement a unique constraint on a has_and_belongs_to_many
relationship like so:
class User
has_and_belongs_to_many :foos, uniq: true
end
Because I only want unique foos
when I call user.foos
, I added the uniq
option. Since upgrading to Rails 4 I've started to get the following warning:
DEPRECATION WARNING: The following options in your User.has_and_belongs_to_many :foos 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'
I've tried a number of different combinations, and read through the source, but can't figure out how to write the unique constraint to remove the warning?
class User
has_and_belongs_to_many :foos, -> { uniq }
end
According to the documentation here
这篇关于在Rails的4 has_and_belongs_to_many关系使用的uniq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!