2012-12-03 102 views
1

希望標題有一定意義,但我會進入更多細節。我有一個簡單的應用程序,允許用戶上傳食譜。然後,您可以查看所有這些或單獨查看每個配方通過show動作顯示與正在顯示頁面上顯示的另一個對象相似的對象

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> 

我想達成什麼是在同一頁上有一個部分,將列出其他食譜的名稱,這些食譜就像顯示食譜一樣,基於dish_name

這是我第一次做這樣的事情,所以我正在尋找一些指向什麼資源看看或如何去一個布特這個

我的想法到目前爲止是

1)創建調用基礎上,dish_name一個範圍的方法顯示,經過dish_name的PARAMS。

這可能是錯誤的,只是爲了尋找在正確的方向輕推,請

編輯

我自己也嘗試這在我的表演動作,但我得到錯誤的參數數目(1 0)

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

感謝

回答

2

如果我這樣做,我想插入我的應用程序到一個全文檢索數據庫,如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 
+0

感謝您的建議,我已閱讀,但它沒有處理相關的模型?是對的嗎? – Richlewis

+0

啊我的錯誤似乎有一種叫multi_solr_search的方法 – Richlewis

+0

你介意給我一個例子讓我開始請嗎?感謝您的幫助 – Richlewis