2012-09-28 75 views
0

我想做一個烹飪網站,但不知道正確的是建立數據庫。如何創建烹飪站點的正確數據庫方案?

我的模特是:RecipeIngredient

食譜中的成分應該是自動填充字段。問題是用戶可以放置任何文本。 (「黃瓜」或「cucamber」),它將成爲不同的成分。

我想通過配料和鏈接進行搜索。什麼是最好的方式來做到這一點?

回答

4

一個配方有很多項目,這反過來又保持一個成分,數量和措施類型的參考。所以,你可以去:

rails generate model Recipe name:string description:text 
rails generate model Item recipe:references ingredient:references amount:decimal measure:string 
rails generate model Ingredient name:string 

,然後添加到您的類:

class Recipe < ActiveRecord::Base 
    has_many :items 
    has_many :ingredients, :through => :items 


    # this allows things like @recipes = Recipe.using("cucumber") 
    scope :using, lambda do |text| 
    joins(:ingredients).where("ingredients.name LIKE ?", "%#{text}%") 
    end 
end 

class Item < ActiveRecord::Base 
    belongs_to :recipe 
    belongs_to :ingredient 

    VALID_MEASURES = %w[oz kg tbsp] # use for "select" tags in forms 
    validates :measure, :inclusion => VALID_MEASURES 
end 

class Ingredient < ActiveRecord::Base 
    belongs_to :item 
end 

從這裏,你開始建立你的觀點,自動完成,無論你的想象力允許。