Rails:一种继承的注释模型与两种略有不同的注释模型

发布于 12-22 18:27 字数 1447 浏览 2 评论 0原文

我有当前的(简化的)模型设置 - 基本上是两个非常不同的模型:

Product
- Title

Restaurant
- Title

Comment
- Message
- gps_cords (sometimes?!)

我的目标是让人们根据此标准随意对 productsrestaurants 发表评论:

  1. 当有人对产品发表评论时,评论应该只有一条消息
  2. 当有人对餐厅发表评论时,评论应该包含消息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

考虑到以下因素,对此进行建模的正确方法是什么:

  1. 性能
  2. 查询“全部”的能力评论”
  3. “铁路方式”? (如果有)
  4. 索引会去哪里?

非常感谢那些花时间阅读所有这些内容的人。

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:

  1. When someone comments on a product the comment should simply have a message.
  2. When someone comments on a restaurant the comment should have a message AND a gps_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:

  1. Performance
  2. Ability to query for "All comments"
  3. "Rails way" ? (if there is)
  4. Where would indexes go?

Thank you very much to the people who take time to read all of this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑2024-12-29 18:27:12

使用单表继承 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文