Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:在创建新模型时创建动态数量的关系_Ruby On Rails_Ruby On Rails 4_Relational Database - Fatal编程技术网

Ruby on rails Rails:在创建新模型时创建动态数量的关系

Ruby on rails Rails:在创建新模型时创建动态数量的关系,ruby-on-rails,ruby-on-rails-4,relational-database,Ruby On Rails,Ruby On Rails 4,Relational Database,在我们的应用程序中,用户可以通过会员身份加入赌注。每个赌注可以有多个回合,每一轮都有许多玩家(用户),这取决于在创建回合时谁是赌注的成员 一轮有许多玩家通过合同(轮和用户之间的关系)。基本上,我希望能够创建一个新的回合,并自动创建一个合同的每个用户谁是打赌的成员。会员可以加入和退出投注,任何投注都只能是他们拥有投注会员资格时创建的回合的一部分 我是Rails的新手,想不出一种方法来自动为每个有会员资格的用户建立契约关系。任何想法都将不胜感激 class Bet < ActiveRecord

在我们的应用程序中,用户可以通过会员身份加入赌注。每个赌注可以有多个回合,每一轮都有许多玩家(用户),这取决于在创建回合时谁是赌注的成员

一轮有许多玩家通过合同(轮和用户之间的关系)。基本上,我希望能够创建一个新的回合,并自动创建一个合同的每个用户谁是打赌的成员。会员可以加入和退出投注,任何投注都只能是他们拥有投注会员资格时创建的回合的一部分

我是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-Bet{where(成员身份:{accepted:true})。where(成员身份:{frost:false}}),通过::成员身份,源::用户
拥有{u-many:反对{u成员,->{where(成员身份:{accepted:true})。where(成员身份:{frost:true}}),通过::成员身份,源::用户
#
有很多:同意请求者,->{where(成员身份:{accepted:false})。where(成员身份:{frost:false}}),通过::成员身份,源::用户
has_many:针对请求者,->{where(成员身份:{accepted:false})。where(成员身份:{frost:true}}),通过::成员身份,源::用户
def成员
同意|反对|成员
结束
#
def请求
同意请求者反对请求者
结束
有很多:轮
结束
============

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
class Round{where(合同:{agreements:true,agree\u wins:true,signed:false}}),通过::合同,源::用户
有很多潜在的失败者,->{其中(合同:{同意:真,同意{赢:假,签名:假}}),通过::合同,来源::用户
通过::contracts,source::user有{u many:winners,->{where(contracts:{agreements:true,agree{u wins:true,signed:true}}}}
有很多输家,->{其中(合同:{agreements:true,agree\u wins:false,signed:true}}),通过::合同,源::用户
结束
==============

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
class用户{where(成员身份:{accepted:true}).where(成员身份:{country:false}}),通过::成员身份,来源::bet
have_-many:against_-bets,->{where(成员身份:{accepted:true})。where(成员身份:{against:true}}),通过::成员身份,来源::bet
通过::memberships,source::bet,有_many:pending_bets,->{where(成员身份:{accepted:false}}}}
def主动下注
同意下注;反对下注
结束
def加入了投注
有效投注|未决投注
结束
#回合(和合同)
你有很多合同吗
#忽略此逻辑,尚未设置且不相关
have_many:agree_maybe_wins,->{where(契约:{agree:true}).where(成员身份:{country:false}}),through::contracts,source::round
have\u many:country\u maybe\u wins,->{where(契约:{agree:true})。where(成员身份:{country:false}}),通过::契约,源::轮
have_many:agree_maybe_lose,->{where(契约:{agree:true})。where(成员身份:{frost:false}}),通过::契约,源::轮
have_many:against_maybe_lose,->{where(契约:{agree:true})。where(成员身份:{against:false}}),通过::契约,源::轮
have_many:agree_wins,->{where(契约:{agree:true}).where(成员身份:{country:false}}),通过::契约,源::轮
have_many:country_wins,->{where(契约:{agree:true})。where(成员身份:{country:false}}),通过::契约,源::轮
有很多:同意损失,->{where(合同:{agree:true})。where(会员资格:{反对:false}}),通过::合同,来源::轮
拥有{u-many:反对}{u损失,->{where(合同:{agree:true})。where(会员资格:{反对:false}}),通过::合同,来源::轮
def潜势ê赢
同意你可能赢;反对你可能赢
结束
def获胜
同意|赢|反对|赢
结束
def潜在损失
同意你可能赢;反对你可能赢
结束
def损失
同意|赢|反对|赢
结束
结束

这里的一种方法是
有很多:通过关系。如果我正确理解您的应用程序,您的模型关系可以大致概括为:

用户->合同->回合->下注

通过这种方式,您可以在
Round
模型中将成员身份作为别名处理

Class Round
  has_many :memberships, class_name: "User", through: :contracts
end
使用包含
user\u id
round\u id
的格式的
Contract
模型,每次用户参与
round
时,您都会创建一个
Contract
模型,将该参与表示为“成员”。换句话说,创建合同不是一个额外的步骤,而是让用户进入回合的基本动作。具体来说,您可以编写以下内容:

class User
  def self.add_to_round(round_id)
    Contract.create(user_id: self.id, round_id: round_id)
  end
end
然后查询
Round.first.members
User.first.rounds

为方便起见,您可能还希望创建另一个
has\u many:through
关系,该关系允许您直接从用户查询赌注(或相反)。这看起来像:

class User
  has_many :rounds, through: :contracts
  has_many :bets, through: :rounds
end

有了这两种关系,您应该能够调用
用户。首先。下注
以获得用户参与的所有下注。

我希望将一轮合同与一轮下注的会员资格完全分开,因为有人可以通过创建会员资格加入一次下注,玩几轮,并通过摧毁他们的会员资格来退出赌注。如果一个投注会员破坏了他们的会员资格,我仍然希望他们有一个历史记录