ruby-on-rails – 在Rails 4中的has_and_belongs_to_many关系中使用uniq

ruby-on-rails – 在Rails 4中的has_and_belongs_to_many关系中使用uniq,第1张

概述我正在尝试在has_and_belongs_to_many关系上实现一个唯一约束,如下所示: class User has_and_belongs_to_many :foos, uniq: trueend 因为当我调用user.foos时我只想要独特的foos,我添加了uniq选项.自升级到Rails 4以来,我开始收到以下警告: DEPRECATION WARNING: The follo 我正在尝试在has_and_belongs_to_many关系上实现一个唯一约束,如下所示:

class User  has_and_belongs_to_many :foos,uniq: trueend

因为当我调用user.foos时我只想要独特的foos,我添加了uniq选项.自升级到Rails 4以来,我开始收到以下警告:

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:

06001

should be rewritten as the following:

06002

我已经尝试了许多不同的组合,并通读源,但无法弄清楚如何编写唯一约束来删除警告?

解决方法
class User  has_and_belongs_to_many :foos,-> { uniq }end

根据documentation here

总结

以上是内存溢出为你收集整理的ruby-on-rails – 在Rails 4中的has_and_belongs_to_many关系中使用uniq全部内容,希望文章能够帮你解决ruby-on-rails – 在Rails 4中的has_and_belongs_to_many关系中使用uniq所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1257660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存