加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 01:51:40 所属栏目:百科 来源:网络整理
导读:我正在尝试在has_and_belongs_to_many关系上实现一个唯一约束,如下所示: class User has_and_belongs_to_many :foos,uniq: trueend 因为当我调用user.foos时我只想要独特的foos,我添加了uniq选项.自升级到Rails 4以来,我开始收到以下警告: DEPRECATION WAR
我正在尝试在has_and_belongs_to_many关系上实现一个唯一约束,如下所示:

class User
  has_and_belongs_to_many :foos,uniq: true
end

因为当我调用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读