0

料理サイトを作りたいのですが、データベースを作るのが正解でした。

私のモデルは:RecipeIngredient.

レシピの材料はオートコンプリート フィールドにする必要があります。問題は、ユーザーがそこに任意のテキストを配置できることです。(「キュウリ」または「キュウリ」)とは別の食材になります。

食材とリンクで検索したい。それを行う最良の方法は何ですか?

4

1 に答える 1

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

ここから、想像力が許す限り、ビュー、オートコンプリートの構築を開始します。

于 2012-09-28T14:50:53.987 に答える