【问题标题】:The association `has_and_belongs_to_many` has an option 'uniq'?关联“has_and_belongs_to_many”有一个选项“uniq”?
【发布时间】:2016-12-20 15:44:06
【问题描述】:

我正在通过“Rails 4 in Action”一书学习 rails,它说协会 has_and_belongs_to_many 有一个选项 uniq。但它似乎不像它所说的那样工作。 下面的类意味着应该为每张票只检索唯一的标签,但所有重复的标签都不像书上说的那样被检索。

class Ticket < ActiveRecord::Base
  ...
  has_and_belongs_to_many :tags, uniq: true
  ...

我现在怀疑关联 has_and_belongs_to_many 是否有选项 uniq。我猜它没有,我在活动记录关联文档中检查了这一点。 http://guides.rubyonrails.org/association_basics.html

【问题讨论】:

    标签: ruby-on-rails activerecord associations has-and-belongs-to-many


    【解决方案1】:

    应该可以了:

    has_and_belongs_to_many :tags, -> { uniq }
    

    【讨论】:

    • 您的代码可以神奇地工作。也许uniq: true 已被弃用或其他什么。
    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多