2017-05-31 101 views
0

我有一個用戶類,它可以讓人們註冊和玩遊戲;我也有一個遊戲類,其中包含該遊戲(RPS)的邏輯。跟蹤勝與用psql損失/ DataMapper的

當人們登記,他們的信息是在PSQL數據庫中保存。該信息是使用獲得的PARAMS。它看起來像這樣:

Class User 

    attr_reader :weapon 

    include DataMapper::Resource 

    property :id,  Serial 
    property :name, String, required: true 
    property :email, String, required: true, unique: true 
    property :password_digest, Text 
    attr_reader :password 
    attr_accessor :password_confirmation 

    validates_confirmation_of :password 
    validates_format_of :email, as: :email_address 

    has n, :games 

相應的遊戲類,包含此數據庫的邏輯:

class Game 

    include DataMapper::Resource 

    property :id,  Serial 
    property :win, ? 
    property :lose, ? 

    belongs_to :user 

我的問題是,我真的不知道如何保持多少遊戲用戶的記錄贏得了/丟失。我需要(或者我應該有輸贏單獨的類?我應該使用什麼密鑰類型(串口/ INT)?我要的是「雙贏」或「失去」由一個每次遞增玩家...好了,輸贏。

共享所有幫助/知識是極大的讚賞。

感謝。

回答

0

一種方式做到這一點可能是添加columsn「勝」和「損失」到用戶的兩個整,並使幫助方法增加:

property :wins, Integer, default: 0 
property :losses, Integer, default: 0 

# usage: user.increment(:wins) or user.increment(:losses) 
def increment(type) 
    update({ type => (send(:type) + 1) }) 
end