0

二対一の継承コメントモデル少し異なりコメントモデル - 基本的に2つの非常に異なるモデル:レール:私は現在(簡体字)モデルのセットアップ持って

Product 
- Title 

Restaurant 
- Title 

Comment 
- Message 
- gps_cords (sometimes?!) 

私の目標は、人々が両方productsにコメントを残すようにすることですがそして、この基準に基づいて意志でrestaurants:誰かが製品にコメントすると

  1. コメントは単にmessageを持っている必要があります。
  2. 誰かがレストランにコメントするとき、コメントにはmessagegps_cordsの値が必要です。

これらは私が検討しているものです:

シナリオ1:一つの大きなお尻のテーブル

class Product < ActiveRecord::Base 
    has_many :product_comments 
end 

class Restaurant < ActiveRecord::Base 
    has_many :restaurant_comments 
end 

class Comment < ActiveRecord::Base 
    # message -> string 
    # gps_cords -> string 
    # type -> string 
end 

class ProductComment < Comment 
    # only uses message 
    belongs_to :product 
end 

class RestaurantComment < Comment 
    # uses message AND gps_cords 
    belongs_to :restaurant 
end 

継承モデル/ Wシナリオ2:2つのコメント・モデル

/wの "重複" の取り組みを
class Product < ActiveRecord::Base 
    has_many :product_comments 
end 

class Restaurant < ActiveRecord::Base 
    has_many :restaurant_comments 
end 

class ProductComment < ActiveRecord::Base 
    # message -> string 
    belongs_to :product 
end 

class RestaurantComment < ActiveRecord::Base 
    # gps_cords -> string 
    # message -> string 
    belongs_to :restaurant 
end 

これを考慮してモデル化する正しい方法は次のとおりです。

  1. "すべてのコメント"
  2. "Railsの道" を照会するパフォーマンス
  3. 能力? (存在する場合)
  4. インデックスはどこに行きますか?

このすべてを読む時間がかかる人には、ありがとうございます。

+0

この質問と回答は役に立ちます:http://stackoverflow.com/questions/555668/single-table-inheritance-and-where-to-use-it-in-rails – Finbarr

答えて

0

使用シングルテーブル継承(STI)、シナリオのように1
ここではそれをよりよく理解するためのリンクです:
http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html

また、ことによって、上記のコメントにreffered質問を経ますFinbarr。

+0

私は実際に私の質問を投稿する。なぜ私はSTIを "コメント可能"(多型性)のようなものと使うべきか、 –

関連する問題