2011-08-16 23 views
0

このモデルの関連クラスの割り当てに取り組んでいます。基本的な関連付けは機能しますが、「カテゴリ」ページビューに問題があります。レールモデルの関連付けの問題

カテゴリページ出力は、私は、この値をGETするにはどうすればよい

  • ディッシュ-ID
  • 食器タイトル
  • レストラン・タイトル< =(カテゴリ/ 1 /)すべきですか?

ルール: <% =デバッグ@dishes: - - 料理が一つのカテゴリ に属し、同じ料理は、複数のレストランで

class Category < ActiveRecord::Base 
    has_many :dishes 
end 

class Dish < ActiveRecord::Base 
    belongs_to :category 
end 

class Restaurant < ActiveRecord::Base 
    has_and_belongs_to_many :dishes 
end 

class DishRestaurant < ActiveRecord::Base 
    has_many :restaurants 
    has_many :dishes 
end 

カテゴリーコントローラ

def show 
    @category = Category.find(params[:id]) 
    @dishes = @category.dishes 
    // RESTAURANT TITLE ?? 

respond_to do |format| 
    format.html # index.html.erb 
    format.xml { render :xml => @category } 
end 

カテゴリ表示することができ%>

ヒント役に立った

おかげ

ピート

答えて

1

は適切にあなたのモデルを定義します。

class Dish < ActiveRecord::Base 
    belongs_to :category 
    has_and_belongs_to_many :restaurants 
end 

class Restaurant < ActiveRecord::Base 
    has_and_belongs_to_many :dishes 
end 

これは、暗黙の結合テーブル名 "dishes_restaurants" を使用します。ジョイントモデルが必要なのは、レストラン内の料理の価格など、特定の情報に結合するだけです。あなたのモデルはようにする必要があり、その場合には:あなたが続くどのようなアプローチ

class Dish < ActiveRecord::Base 
    belongs_to :category 
    has_many :dish_restaurants 
    has_many :restaurants, through => :dish_restaurants 
end 

class Restaurant < ActiveRecord::Base 
    has_many :dish_restaurants 
    has_many :dishes, through => :dish_restaurants 
end 

class DishRestaurant < ActiveRecord::Base 
    belongs_to :restaurant 
    belongs_to :dish 
end 

、あなたは料理をサーブレストランのリストを取得するためにdish.restaurantsを行うことができます。

関連する問題