【发布时间】:2015-06-26 20:23:25
【问题描述】:
在我们的应用中,用户可以通过会员资格加入投注。每个投注可以有多个回合,并且每个回合都有许多玩家(用户),这取决于在创建回合时谁是投注的成员。
一个回合通过合约(回合和用户之间的关系)有许多玩家。基本上,我希望能够创建一个新回合并为每个参与投注的用户自动创建一个合同。会员可以加入和离开投注,任何只是在他们拥有投注会员资格时创建的回合的一部分。
我是 Rails 的新手,想不出一种方法来自动为每个拥有赌注会员资格的用户建立合同关系。任何想法都将不胜感激!
class Bet < ActiveRecord::Base
has_many :memberships, dependent: :destroy
#
has_many :agree_members, -> { where(memberships: { accepted: true }).where(memberships: { against: false }) }, through: :memberships, source: :user
has_many :against_members, -> { where(memberships: { accepted: true }).where(memberships: { against: true }) }, through: :memberships, source: :user
#
has_many :agree_requesters, -> { where(memberships: { accepted: false }).where(memberships: { against: false }) }, through: :memberships, source: :user
has_many :against_requesters, -> { where(memberships: { accepted: false }).where(memberships: { against: true }) }, through: :memberships, source: :user
def members
agree_members | against_members
end
#
def requests
agree_requesters | against_requesters
end
has_many :rounds
end
============
class Round < ActiveRecord::Base
belongs_to :bet
has_many :contracts, dependent: :destroy
has_many :potential_winners, -> { where(contracts: { agrees: true, agree_wins: true, signed: false }) }, through: :contracts, source: :user
has_many :potential_losers, -> { where(contracts: { agrees: true, agree_wins: false, signed: false }) }, through: :contracts, source: :user
has_many :winners, -> { where(contracts: { agrees: true, agree_wins: true, signed: true }) }, through: :contracts, source: :user
has_many :losers, -> { where(contracts: { agrees: true, agree_wins: false, signed: true }) }, through: :contracts, source: :user
end
==============
class User < ActiveRecord::Base
# BETS (and bet memberships)
has_many :memberships
has_many :agree_bets, -> { where(memberships: { accepted: true }).where(memberships: { against: false }) }, through: :memberships, source: :bet
has_many :against_bets, -> { where(memberships: { accepted: true }).where(memberships: { against: true }) }, through: :memberships, source: :bet
has_many :pending_bets, -> { where(memberships: { accepted: false }) }, through: :memberships, source: :bet
def active_bets
agree_bets | against_bets
end
def joined_bets
active_bets | pending_bets
end
# ROUNDS (and contracts)
has_many :contracts
#IGNORE THIS LOGIC, NOT SET UP YET AND NOT RELEVANT
has_many :agree_maybe_wins, -> { where(contracts: { agree: true }).where(memberships: { against: false }) }, through: :contracts, source: :round
has_many :against_maybe_wins, -> { where(contracts: { agree: true }).where(memberships: { against: false }) }, through: :contracts, source: :round
has_many :agree_maybe_loses, -> { where(contracts: { agree: true }).where(memberships: { against: false }) }, through: :contracts, source: :round
has_many :against_maybe_loses, -> { where(contracts: { agree: true }).where(memberships: { against: false }) }, through: :contracts, source: :round
has_many :agree_wins, -> { where(contracts: { agree: true }).where(memberships: { against: false }) }, through: :contracts, source: :round
has_many :against_wins, -> { where(contracts: { agree: true }).where(memberships: { against: false }) }, through: :contracts, source: :round
has_many :agree_losses, -> { where(contracts: { agree: true }).where(memberships: { against: false }) }, through: :contracts, source: :round
has_many :against_losses, -> { where(contracts: { agree: true }).where(memberships: { against: false }) }, through: :contracts, source: :round
def potential_wins
agree_maybe_wins | against_maybe_wins
end
def wins
agree_wins | against_wins
end
def potential_losses
agree_maybe_wins | against_maybe_wins
end
def losses
agree_wins | against_wins
end
end
【问题讨论】:
-
我希望将回合的合同与投注的会员资格完全分开,因为有人可以通过创建会员资格加入投注,玩几回合,然后通过销毁他们的会员资格退出投注。如果一个投注会员破坏了他们的会员资格,我仍然希望他们通过他们参与的所有轮次的合同来记录投注历史
标签: ruby-on-rails ruby-on-rails-4 relational-database