Rails:一种继承的注释模型与两种略有不同的注释模型
我有当前的(简化的)模型设置 - 基本上是两个非常不同的模型:
Product
- Title
Restaurant
- Title
Comment
- Message
- gps_cords (sometimes?!)
我的目标是让人们根据此标准随意对 products
和 restaurants
发表评论:
- 当有人对产品发表评论时,评论应该只有一条
消息
。 - 当有人对餐厅发表评论时,评论应该包含
消息
和gps_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
场景 2:带有两个注释模型的“重复”工作
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
考虑到以下因素,对此进行建模的正确方法是什么:
- 性能
- 查询“全部”的能力评论”
- “铁路方式”? (如果有)
- 索引会去哪里?
非常感谢那些花时间阅读所有这些内容的人。
I have the current (simplified) Model setup -- basically two very different models:
Product
- Title
Restaurant
- Title
Comment
- Message
- gps_cords (sometimes?!)
My goal is to let people leave comments on both products
and restaurants
at will based on this criteria:
- When someone comments on a product the comment should simply have a
message
. - When someone comments on a restaurant the comment should have a
message
AND agps_cords
value.
These are what I'm considering:
Scenario 1: One big ass table w/ inheritance models
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
Scenario 2: "Duplicate" efforts w/ two comment models
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
What is the correct way to model this giving consideration to:
- Performance
- Ability to query for "All comments"
- "Rails way" ? (if there is)
- Where would indexes go?
Thank you very much to the people who take time to read all of this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

使用单表继承 (STI),如场景 1 中所示。
这是一个可以更好地理解它的链接:
http://code.alexreisner.com/articles/single-table -inheritance-in-rails.html
另外,请仔细检查 Finbarr 在上面的评论中提到的问题。
Use Single Table Inheritance (STI), as in the scenario 1.
Here is a link for a better understanding of it:
http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html
Also, go through the question reffered in the comment above by Finbarr.