我有一個明星示範模型,包括total_stars
和average_stars
列於郵政型號:如何強制一列只允許以這種方式編號:1,1.5,2,2.5等等?
create_table "posts", force: true do |t|
t.string "title"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.integer "average_stars", default: 0, null: false
t.integer "total_stars", default: 0, null: false
end
高清calculate_total_stars 如果[發佈] .INCLUDE(starable.class) 自我? starable.update_column(:total_stars,starable.total_stars + self.number) 端端
DEF calculate_average_stars ,如果[郵政] .INCLUDE(starable.class) self.starable.update_column?(:average_stars,starable.total_stars/starable.stars.count) 端端
所以,現在的問題是,如果average_stars
是3.6
最終結果只是3
。我不太確定什麼樣的計算或近似適用於五星評級系統。但我希望按以下方式進行:1,1.5,2,2.5 ...
有關如何修改average_stars
列以實現該結果的任何建議?
您想存儲1.5,2.5,但該列被定義爲整數。首先更改average_stars列的數據類型 –