[英]Question about Ruby on Rails, Constants, belongs_to & Database Optimization/Performance
[英]performance/ruby/rails/db question
我正在創建一個在線博彩賠率比較網站,並且想知道如何計算Ruby / Rails中的最佳賠率。
我有兩個模型:比賽和賠率
Fixture有主場和客場球隊,賠率模型有庄家ID,主場賠率,平局賠率和客場賠率。
我有一些選擇只將選定的燈具/團隊存儲在數據庫中。
我正在考慮以這種方式創建一個由不同庄家組成的多維數組,然后添加fixture_id
和1,2或3進行主頁/抽簽/ fixture_id
牌,然后將其用作添加賠率的關鍵
像odds[bookmaker][fixture][1/2/3] = price
然后加總odds = count(odds[bookmaker][fixture][1/2/3])
?
有更容易的方法嗎? 也許在數據庫中做?
在不考慮性能的情況下-可能不是問題,無論如何,我們不應該針對性能進行優化,直到我們知道存在問題-我要說您可能會引入Bookmaker模型(如果只是為了存儲名稱)並開始制作使用ActiveRecord關聯。 我還將考慮將賠率分為三個單獨的結果類型,這可能會更加靈活,特別是如果您以后要添加更多的賭注。 您可能會得到類似:
class Bookmaker < ActiveRecord::Base
has_many :odds
end
class Odd < ActiveRecord::Base # good name? Price is almost as common and less likely to be misinterpreted
belongs_to :fixture
belongs_to :bookmaker
# let's assume we use result type = 1/2/3 or maybe :home/:draw/:away
end
class Fixture < ActiveRecord::Base
has_many :odds
end
您想要做的是為所有在該固定裝置上定價的商品(或“綜合”)中的每個結果計算最佳價格。 如果小於100%,則存在潛在套利。
class Odd
named_scope :for_result, lambda { |res_tp| {:conditions => ['type = ?', res_tp]}}
end
class Fixture
def best_price(res_type)
# assumes you have odds stored as a percentage
odds.for_result(res_type).minimum(:pctage)
end
def overround
[:home, :away, :draw].inject(0.0){|sum, res_tp| sum + best_price(res_tp)}
end
end
我敢保證上面的數據不完全適合您的數據,但是它可能會給您一個思路。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.