【问题标题】:Display objects that are LIKE another object that is being displayed on show page显示类似于显示页面上显示的另一个对象的对象
【发布时间】:2012-11-21 08:08:15
【问题描述】:

希望标题有点意思,但我会详细介绍。我有一个简单的应用程序,允许用户上传食谱。然后,您可以通过显示操作查看全部或单独查看每个配方

def show
@recipe = Recipe.find(params[:id])
end

然后我在视图中显示该食谱的各种属性,如下所示

  <b><%= @recipe.dish_name %></b><br>
  <b><%= image_tag @recipe.avatar.url(:showrecipe) %><b><br>
  <b>by&nbsp;<%= @recipe.user.name %></b></br></br>
  <b>Description</b></br>
  <b><%= @recipe.description %></b></br>
  <b>Ingredients</b></br>
  <b><%= raw(ingredient_names_list(@recipe.ingredients)) %></b>
  <br>
  <b>Preperation Steps</b></br>
  <ol>
  <li><%= raw(preperation_steps_list(@recipe.preperations)) %></li>
  </ol>

我想要实现的是在同一页面上有一个部分,该部分将根据 dis_name 列出与显示的食谱类似的其他食谱的名称

这是我第一次做这样的事情,所以我正在寻找一些关于要查看哪些资源或我将如何去做的指针

到目前为止,我的想法是

1) 创建一个基于显示的菜名调用作用域的方法,传递菜名的参数。

这可能是错误的,请在正确的方向上轻推

编辑

我也在我的表演动作中尝试过这个,但我得到了错误的参数数量(1 代表 0)

@relatedrecipe = Recipe.where(@recipe.dish_name('LIKE'),(params[:dish_name]))

谢谢

【问题讨论】:

    标签: ruby-on-rails-3 methods scopes


    【解决方案1】:

    如果我这样做,我会将我的应用程序插入全文搜索数据库,例如 sunspot_solrsunspot_rails,并索引标题、描述和成分列表。

    然后使用标准化版本的标题和成分创建一个通用搜索,排除您已经查看的记录,以提供近乎命中和相关内容。

    编辑使用sunspot_rails(我有一些经验)的更具体示例:

    sunspot_rails 使用模型中的可搜索块来告诉它应该在创建/更新/销毁时索引什么。 您可以使用 :ingredient_names 创建如下所示的自定义索引。

    class Recipe < ActiveRecord::Base
    
      searchable do
        integer :id
        string  :description
        text    :preparations
        text    :ingredient_names
      end
    
      def ingredient_names
        ingredients.map{|i| i.name }.uniq.compact.join(' ')
      end
    
      def similar_recipes
        search = Recipe.search do
          without(:id, self.id) # don't return the same record
          fulltext(self.description) do
            boost_fields(:description => 2) # give extra weight to description matches
          end
          fulltext(self.ingredient_names)
        end
        search.results
      end
    
    end
    

    【讨论】:

    • 感谢您的建议,虽然我读过它不涉及相关模型?对吗?
    • 啊我的错误似乎有一个名为multi_solr_search的方法
    • 你介意给我一个例子让我开始吗?感谢您的帮助
    • @Richlewis 更新了一个例子。在此处查看文档:github.com/sunspot/sunspot#readme
    猜你喜欢
    • 2019-05-11
    • 2014-06-07
    • 2010-10-11
    • 1970-01-01
    • 2017-09-30
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多