1

Hopefully the title makes some sense but I will go into more details. I have a simple app that allows users to upload recipes. You can then view them all or view each recipe individually via the show action

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

In the view i then display various attributes for that recipe like so

  <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>

What I would like to achieve is to have a section on the same page that will list the names of other recipes that are LIKE the recipe being shown, based on dish_name

This is my first time doing something like this so i am looking for some pointers on what resources to look at or how i would go about this

My thinking so far is to

1) Create a method that calls a scope based on the dish_name being displayed, passing the params of the dish_name.

That may be wrong, just looking for a nudge in the right direction please

EDIT

I have also tried this in my show action but i get wrong number of arguments (1 for 0)

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

Thanks

Richlewis
  • 15,070
  • 37
  • 122
  • 283

1 Answers1

2

If I were doing this, I would plug my app into a full-text search database, such as sunspot_solr with sunspot_rails and index the titles, description, and list of ingredients.

Then use a normalized version of the title and ingredients to create a generic search excluding the record you are already looking at to come up with near-hits and related content.

Edit for a more concrete example using sunspot_rails (something I have some experience with):

sunspot_rails uses a searchable block in your model to tell it what it should index on create/update/destroy. You can create custom indexes like I show below with :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
Unixmonkey
  • 18,485
  • 7
  • 55
  • 78