具有多个模型的太阳黑子(在 Rails 中)

发布于 2024-11-07 17:13:46 字数 535 浏览 2 评论 0原文

我有三个模型(UserTagProduct),它们交互 st User 有许多 Tags产品

出于搜索目的,我希望能够(使用一个搜索栏)搜索用户名、标签名称和产品描述。我还想在产品页面上搜索,但这仅与标签名称和产品描述相关。

以下是两个示例:

搜索:“Linus Torvalds”会返回三个模型中 Linus Torvalds 的所有实例,并且用户名的任何实例均位于较高位置。

搜索:年龄为“20-25”的“Linux”会返回名称/描述中包含“Linux”且属于该年龄范围的产品的所有用户实例,以及标签包含“Linux”和谁的用户有适合该年龄段的产品。请注意,如果搜索不包括年龄,那么它将默认搜索所有符合“Linux”部分的人,而不是没有。

我的问题是最好的方法是什么?我应该创建一个带有自己的控制器的搜索模型吗?我应该忽略它并将搜索部分包含在共享文件夹中吗?还有哪些其他方法?

非常感谢。

I have three models (User, Tag, Product) and they interact s.t. User has many Tags and Products.

For searching purposes, I would like to be able to search (with one search bar) on user names, tag names, and product descriptions. I would also like to search on product pages, but that is only relevant for tag names and product descriptions.

Here are two examples:

Search: "Linus Torvalds" returns all instances of Linus Torvalds in the three models with any instances of the user name being placed higher.

Search: "Linux" with age: "20-25" returns all instances of Users with Products that include "Linux" in their name/description and fall in that age range, as well as Users with tags that include "Linux" and who have Products that fall in that age range. Note that if the search didnt include the age, then it would default to all who fit the "Linux" part rather than none.

My question is what would be the best way of doing this? Should I create a search model with its own controller? Should I just ignore that and include a search partial in a shared folder? What other methods are there?

Thanks much.

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

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

发布评论

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

评论(1

墨落成白 2024-11-14 17:13:46

如果您正在执行任何复杂的条件,我喜欢搜索对象的想法。

但要使用太阳黑子搜索对象:

@sunspot_search = Sunspot.search User, Tag, Product do |query| 
  query.keywords @search_query
  query.with(:age).greater_than 20
  query.with(:age).less_than 25
  query.paginate(:page => params[:page], :per_page => 30)
end

I like the idea of a Search object if you are doing any complicated conditions.

But to search across objects using Sunspot:

@sunspot_search = Sunspot.search User, Tag, Product do |query| 
  query.keywords @search_query
  query.with(:age).greater_than 20
  query.with(:age).less_than 25
  query.paginate(:page => params[:page], :per_page => 30)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文