Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 显示与显示页面上显示的另一个对象类似的对象_Ruby On Rails 3_Methods_Scopes - Fatal编程技术网

Ruby on rails 3 显示与显示页面上显示的另一个对象类似的对象

Ruby on rails 3 显示与显示页面上显示的另一个对象类似的对象,ruby-on-rails-3,methods,scopes,Ruby On Rails 3,Methods,Scopes,希望这个标题有点道理,但我会详细介绍。我有一个简单的应用程序,允许用户上传食谱。然后,您可以通过show action查看所有配方或单独查看每个配方 def show @recipe = Recipe.find(params[:id]) end 在视图中,我随后显示该配方的各种属性,如下所示 <b><%= @recipe.dish_name %></b><br> <b><%= image_tag @recipe.avat

希望这个标题有点道理,但我会详细介绍。我有一个简单的应用程序,允许用户上传食谱。然后,您可以通过show action查看所有配方或单独查看每个配方

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>

谢谢

如果我这样做的话,我会将我的应用程序插入一个全文搜索数据库,比如带有
sunspot\u rails
sunspot\u solr
,并为标题、描述和成分列表编制索引

然后,使用标题和成分的标准化版本创建一个通用搜索,不包括您已经在查看的记录,以获得接近点击率和相关内容

编辑,了解使用
太阳黑子轨道
的更具体示例(我有一些经验):

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

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
类配方2)#为描述匹配赋予额外的权重
结束
全文(自身名称)
结束
搜索结果
结束
结束

谢谢你的建议,我已经读到它不处理相关的模型?是这样吗?啊,我的错误似乎是有一种叫做multi_solr_search的方法,你介意给我举个例子让我开始吗?感谢你help@Richlewis更新了一个例子。检查此处的文档:
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