使用metawhere 搜索标签

发布于 2024-10-16 13:41:23 字数 748 浏览 2 评论 0原文

我正在尝试对模型及其关联标签执行多列搜索。我正在使用metawhere 并充当可标记的角色。基本上我有一个带有标题和正文的模型,其中有一定数量的带有名称的标签。我一直在尝试使用 metawhere 设置此查询,但当我尝试加入模型的标签时,它永远不会返回任何结果。我有一个名为“str”的变量,用于搜索带有以下内容的 Post 模型...

Post.where(:title.matches % '#{str}%' | :body.matches % '#{str}%' | {:tags => [:name.matches % '#{str}%']}).joins(:tags)

生成以下 sql...

=> "SELECT `posts`.* FROM `posts` INNER JOIN `taggings` ON `posts`.`id` = `taggings`.`taggable_id` AND `taggings`.`taggable_type` = 'Post' INNER JOIN `tags` ON 'taggings.tagger_id IS NULL AND taggings.context = \\'tags\\'' WHERE (((`posts`.`title` LIKE '\#{str}%' OR `posts`.`body` LIKE '\#{str}%') OR `tags`.`name` LIKE '\#{str}%'))"

任何人都可以用这个为我指出正确的方向吗?任何帮助将不胜感激。

I'm trying to perform a multi-column search on a model and it's associated tags. I'm using metawhere and acts-as-taggable-on for this. Basically I have a model with a title and body that has a certain amount of tags with names. I've been trying to set this query up with metawhere but it never returns any results when I try to join to a model's tags. I have a variable named "str" that's being used to search a Post model with the following...

Post.where(:title.matches % '#{str}%' | :body.matches % '#{str}%' | {:tags => [:name.matches % '#{str}%']}).joins(:tags)

which generates the following sql...

=> "SELECT `posts`.* FROM `posts` INNER JOIN `taggings` ON `posts`.`id` = `taggings`.`taggable_id` AND `taggings`.`taggable_type` = 'Post' INNER JOIN `tags` ON 'taggings.tagger_id IS NULL AND taggings.context = \\'tags\\'' WHERE (((`posts`.`title` LIKE '\#{str}%' OR `posts`.`body` LIKE '\#{str}%') OR `tags`.`name` LIKE '\#{str}%'))"

Can anyone point me in the right direction with this? Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-10-23 13:41:23

尝试:(假设启用了运算符重载)

.where((:title =~ str) | (:body =~ str) | {:tags => [:name =~ str]})

Try: (assuming operator overloads are enabled)

.where((:title =~ str) | (:body =~ str) | {:tags => [:name =~ str]})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文