将meta_search 与acts_as_taggable_on 结合起来
我正在开发的 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
,所以我可能在这里做错了什么。 ;) 我在某处读到 searchlogic
与 acts_as_taggable_on
结合使用,但由于它不支持 Rails 3,所以我无法使用它。
我希望这里有人能帮助我解决这个问题。有人知道如何将 acts_as_taggable_on
与 meta_search
结合起来吗?或者也许知道没有 meta_search
的解决方案?另外,如果 acts_as_taggable_on
有更好的选项可以与 meta_search
配合使用,我也很想听听。 :)
编辑: 我在没有使用 tagged_with
或 meta_search
的情况下让它工作。它看起来像这样:
Post.select("DISTINCT posts.*").joins(:base_tags).where("posts.title LIKE ? OR tags.name = ?", "%"+params[:search]+"%", params[:search])
创建的查询很荒谬,所以我尝试了不同的路线:没有 acts_as_taggable_on
和 meta_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
享受
enjoy
我认为您正在研究的解决方案是使用数据库 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.