将meta_search 与acts_as_taggable_on 结合起来

发布于 2024-11-02 21:57:46 字数 2045 浏览 0 评论 0原文

我正在开发的 Rails 3 网站的一些搜索功能遇到了一个小问题。我有一个简单的 Post 模型,如下所示:

class Post < ActiveRecord::Base
  acts_as_taggable
end

我使用 acts_as_taggable_on 来更轻松地向我的帖子添加标签。当我有一篇标记为“rails”的帖子并且执行以下操作时,一切正常:

@posts = Post.tagged_with("rails")

事情是,我还想搜索帖子的标题。当我有一篇标题为“Hello world”并标记为“rails”的帖子时,我希望能够通过搜索“hello”“rails”来找到该帖子。因此,我想要标题列的 LIKE 语句与 tagged_with 方法 acts_as_taggable_on 提供的组合。 where 范围不起作用,因为它使用 AND 而不是 OR

我希望 meta_search 能够解决这个问题,但这对我不起作用。我尝试了几件事。以下是我尝试过的两个示例:

@search = Post.search(:tagged_with_or_title_like => params[:search])
@search = Post.search(:title_like => params[:search], :tagged_with => params[:search])

它只是无法识别“tagged_with”。这是我第一次使用 meta_search,所以我可能在这里做错了什么。 ;) 我在某处读到 searchlogicacts_as_taggable_on 结合使用,但由于它不支持 Rails 3,所以我无法使用它。

我希望这里有人能帮助我解决这个问题。有人知道如何将 acts_as_taggable_onmeta_search 结合起来吗?或者也许知道没有 meta_search 的解决方案?另外,如果 acts_as_taggable_on 有更好的选项可以与 meta_search 配合使用,我也很想听听。 :)

编辑: 我在没有使用 tagged_withmeta_search 的情况下让它工作。它看起来像这样:

Post.select("DISTINCT posts.*").joins(:base_tags).where("posts.title LIKE ? OR tags.name = ?", "%"+params[:search]+"%", params[:search])

创建的查询很荒谬,所以我尝试了不同的路线:没有 acts_as_taggable_onmeta_search。我自己创建了一个标签表,并使用 has_and_belongs_to_many 将其连接到帖子。我不会有其他需要连接到标签的表,所以这对我来说很有效。我创建了一个搜索标签和标题的范围:

scope :search, lambda { |search| select("DISTINCT posts.*").joins(:tags).where("posts.title LIKE ? OR tags.name = ?", "%"+search+"%", search) }

现在我可以执行以下操作:

Post.search(params[:search])

它效果很好,查询也非常好。不过,如果有人知道更好的方法:请告诉我。这对于通过谷歌来到这里的人也可能有帮助。

I've run into a small problem with some search-functionality for a Rails 3 website I'm developing. I have a simple Post model that looks like this:

class Post < ActiveRecord::Base
  acts_as_taggable
end

I'm using acts_as_taggable_on to make adding tags to my posts a bit easier. When I have a post tagged 'rails' and I do the following, all works well:

@posts = Post.tagged_with("rails")

Thing is, I also want to search for the title of the post. When I have a post titled 'Hello world' and tagged 'rails', I want to be able to find this post by searching for 'hello' or 'rails'. So I want a LIKE statement for the title column in combination with the tagged_with method acts_as_taggable_on provides. The where scope doesn't work, because it uses AND instead of OR.

I hoped meta_search would fix the problem, but this isn't working for me. I tried several things. Here are two examples of what I tried:

@search = Post.search(:tagged_with_or_title_like => params[:search])
@search = Post.search(:title_like => params[:search], :tagged_with => params[:search])

It just doesn't recognize 'tagged_with'. It's my first time using meta_search, so I might be doing something wrong here. ;) I've read somewhere that searchlogic worked in combination with acts_as_taggable_on, but since it doesn't support Rails 3, I can't use that.

I hoped somebody here could help me with this problem. Anyone know how to combine acts_as_taggable_on with meta_search? Or maybe know a solution without meta_search? Also, if there's a better option for acts_as_taggable_on that works with meta_search I'd love to hear that too. :)

EDIT:
I got it working without using tagged_with or meta_search. It looked like this:

Post.select("DISTINCT posts.*").joins(:base_tags).where("posts.title LIKE ? OR tags.name = ?", "%"+params[:search]+"%", params[:search])

The query created was ridiculous, so I tried a different route: without acts_as_taggable_on and meta_search. I created a tags table myself and connected it to the posts with has_and_belongs_to_many. I won't have other tables that need to be connected to the tags, so this will do the trick for me. I created a scope for searching both the tags and title:

scope :search, lambda { |search| select("DISTINCT posts.*").joins(:tags).where("posts.title LIKE ? OR tags.name = ?", "%"+search+"%", search) }

Now I can do the following:

Post.search(params[:search])

It works great and the query is also quite nice. Still, if anyone knows a better way: please tell me. It could also be helpful for the people coming here via Google.

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

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

发布评论

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

评论(2

听不够的曲调 2024-11-09 21:57:46
Post.metasearch({:title_or_tag_taggings_tag_name_contains => params[:search]})

享受

Post.metasearch({:title_or_tag_taggings_tag_name_contains => params[:search]})

enjoy

风吹短裙飘 2024-11-09 21:57:46

我认为您正在研究的解决方案是使用数据库 VIEW 作为新模型。

您可以创建包含连接表“帖子”和“标签”的视图。

然后你可以使用meta_search 毫无问题地搜索它。

I think the solution you're looking into is using database VIEW as new model.

You can create view containing join tables Post and Tags.

Then you can search it, using meta_search without problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文