Rails:如果子元素包含任何其他数组,我将如何检索记录



我有一个User和Document模型,它们都有tag_lists(来自acts_as_taggable)

我想查找具有某个用户的任何标记的文档。例如,

user.tag_list = ["ceo" , "sales"]
documentA.tag_list = ["ceo"] 
documentB.tag_list = ["all-users'] 
documentC.tag_list = ["sales", "marketing"] 
documentD.tag_list = ["marketing"]

我想做一些类似的事情

Document.where(tag_list.includes_any?("all-users" || user.tag_list))
->其将检索文档A、B、C。

显然语法是错误的,但你明白了。我该如何有效地做到这一点?

-------更新

请注意,tag_list不是User或Document模型的直接属性,而是来自acts_as_taggable gem的活动记录关系:我如何在.where运算符中调用它?

2.1.1 :036 > Document.last.tag_list
  Document Load (3.7ms)  SELECT  "assignable_objects".* FROM "assignable_objects"  WHERE "assignable_objects"."type" IN ('Document')  ORDER BY "assignable_objects"."id" DESC LIMIT 1
  ActsAsTaggableOn::Tag Load (3.5ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 52], ["taggable_type", "AssignableObject"]]
 => []

这可能会对您有所帮助:

    Document.where((tag_list-["all-users", user.tag_list].flatten).size != tag_list.size)

相关内容

  • 没有找到相关文章

最新更新