0

我有電流(簡體)模式設置 - 基本上兩個非常不同的模式:Rails的:一是繼承評論模型VS兩個稍微不同的評價模型

Product 
- Title 

Restaurant 
- Title 

Comment 
- Message 
- gps_cords (sometimes?!) 

我的目標是讓人們離開兩個products評論和restaurants隨意在此基礎上的標準:

  1. 當有人在產品評論的評論應該只是有一個message
  2. 當有人對餐廳發表評論時,評論應該有messagegps_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 

建模時需要考慮的正確方法是什麼:

  1. 性能
  2. 能夠查詢 「所有評論」
  3. 「Rails的路」? (如果有的話)
  4. 索引在哪裏去?

非常感謝那些花時間閱讀所有這些內容的人。

+0

你可能會發現這個問題和答案很有用︰http://stackoverflow.com/questions/555668/single-table-inheritance-and-where-to-use-it-in-rails – Finbarr 2011-12-26 06:00:59

回答

0

使用單表繼承(STI),如在方案1
這是爲了更好地理解它的鏈接:
http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html

而且,經過在註釋下文稱上面的問題財務巴雷。

+0

我實際上已經讀過張貼我的問題。我不清楚爲什麼我應該使用STI而不是像「可評論的」(多態) – 2011-12-26 18:00:45