我有電流(簡體)模式設置 - 基本上兩個非常不同的模式:Rails的:一是繼承評論模型VS兩個稍微不同的評價模型
Product
- Title
Restaurant
- Title
Comment
- Message
- gps_cords (sometimes?!)
我的目標是讓人們離開兩個products
評論和restaurants
隨意在此基礎上的標準:
- 當有人在產品評論的評論應該只是有一個
message
。 - 當有人對餐廳發表評論時,評論應該有
message
和gps_cords
值。
這些是我在考慮:
情景1:一個大屁股表W /遺傳模型
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
方案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
建模時需要考慮的正確方法是什麼:
- 性能
- 能夠查詢 「所有評論」
- 「Rails的路」? (如果有的話)
- 索引在哪裏去?
非常感謝那些花時間閱讀所有這些內容的人。
你可能會發現這個問題和答案很有用︰http://stackoverflow.com/questions/555668/single-table-inheritance-and-where-to-use-it-in-rails – Finbarr 2011-12-26 06:00:59