Ruby on rails Simple通过查询获得了许多结果

Ruby on rails Simple通过查询获得了许多结果,ruby-on-rails,Ruby On Rails,我通过标记与事件标记关联。 在数组标记=%wtag1 tag2 tag3中,我有标记名。我希望使用此数组中的一个或多个标记来标记所有事件。我如何才能做到这一点?在标签上写一个作用域 class Tag < ActiveRecord::Base def self.with_names(names) where(name: names) end end 如果不希望重复的事件匹配多个标记,则可能需要调用uniq Event.for_tags(tags).uniq 多亏桑蒂亚的

我通过标记与事件标记关联。 在数组标记=%wtag1 tag2 tag3中,我有标记名。我希望使用此数组中的一个或多个标记来标记所有事件。我如何才能做到这一点?

在标签上写一个作用域

class Tag < ActiveRecord::Base
  def self.with_names(names)
    where(name: names)
  end
end
如果不希望重复的事件匹配多个标记,则可能需要调用uniq

Event.for_tags(tags).uniq

多亏桑蒂亚的评论,我终于找到了答案

Event.includes:tags.wheretags:{name:tags}或 Event.includes:tags.wheretags.name IN,tags.references:tags

尝试此事件。includes:tag.wheretag:tags或is关系与事件-tags和Event的数量相同。includes:tags.wheretag:tags在数组的何处
tags = %w(tag1 tag2 tag3)
Event.for_tags(tags)
Event.for_tags(tags).uniq