Ruby on rails Rails模型关系有许多属于

Ruby on rails Rails模型关系有许多属于,ruby-on-rails,activerecord,relationship,Ruby On Rails,Activerecord,Relationship,我有一个模型匹配和一个模型团队,每个匹配有两个团队,每个团队可以有多个匹配 Team: name:string Match name:string team1:references team2:references 所以我的模型是这样的 class Match < ActiveRecord::Base belongs_to :team1, :class_name => Team, :foreign_key => "team1_id" belongs_to :team

我有一个模型
匹配
和一个模型
团队
,每个
匹配
有两个
团队
,每个
团队
可以有多个
匹配

Team: name:string

Match name:string team1:references team2:references
所以我的模型是这样的

class Match < ActiveRecord::Base
  belongs_to :team1, :class_name => Team, :foreign_key => "team1_id"
  belongs_to :team2, :class_name => Team, :foreign_key => "team2_id"
end

class Team < ActiveRecord::Base
  has_many :matches
end
类匹配Team,:foreign\u key=>team1\u id
属于:team2,:class\u name=>Team,:foreign\u key=>team2\u id
结束
类团队

我希望能够通过比赛组建一支新的球队。我既不想要重复的比赛记录,也不想要球队记录。我有点迷路了,如果这个组合是球队和比赛之间的正确组合

这里你应该使用has\u和\u属于\u many关系

Match.rb

class Match < ActiveRecord::Base
  has_and_belongs_to_many :teams
end
class Team < ActiveRecord::Base
  has_and_belongs_to_many :matches
end
然后在生成的迁移文件中:

class CreateMatchTeams < ActiveRecord::Migration
  def self.up
    create_table :matches_teams, :id => false do |t|   # :id => false; is to  prevent the creation of primary key
        t.integer :match_id
        t.integer :team_id
    end
  end

  def self.down
    drop_table :matches_teams
  end
end
class CreateMatchTeamfalse do | t |#:id=>false;是为了防止创建主键
t、 整数:匹配\u id
t、 整数:团队id
结束
结束
def自动关闭
投球桌:比赛队
结束
结束

然后运行此迁移,您可以通过habtm关系将团队和比赛相互关联。

尝试以下操作:

class Match < ActiveRecord::Base
  #home team
  belongs_to :team1, :class_name => Team, :foreign_key => "team1_id"
  #away team
  belongs_to :team2, :class_name => Team, :foreign_key => "team2_id"

  #this should only allow 1 match between each team
  validates :team1_id, :uniqueness => { :scope => :team2_id }
end

class Team < ActiveRecord::Base
  has_many :home_matches, :class_name => Match, :foreign_key => "team1_id"
  has_many :away_matches, :class_name => Match, :foreign_key => "team2_id"

  validates :name, :uniqueness => true

  def matches
    Match.where("team1_id = ? OR team2_id = ?", self.id, self.id)
  end
end
类匹配Team,:foreign\u key=>team1\u id
#客队
属于:team2,:class\u name=>Team,:foreign\u key=>team2\u id
#这应该只允许每个队之间进行一场比赛
验证:team1\u id,:university=>{:scope=>:team2\u id}
结束
类团队匹配项,:外部密钥=>团队1\u id
有很多:客场比赛,:class\u name=>Match,:foreign\u key=>team2\u id
验证:name,:university=>true
def匹配
Match.where(“team1\u id=?或team2\u id=?”,self.id,self.id)
结束
结束

谢谢你的回答,到目前为止它是有效的:)因此,即使一场比赛只能有两支球队,在你看来,habtm关系是最好的?
class Match < ActiveRecord::Base
  #home team
  belongs_to :team1, :class_name => Team, :foreign_key => "team1_id"
  #away team
  belongs_to :team2, :class_name => Team, :foreign_key => "team2_id"

  #this should only allow 1 match between each team
  validates :team1_id, :uniqueness => { :scope => :team2_id }
end

class Team < ActiveRecord::Base
  has_many :home_matches, :class_name => Match, :foreign_key => "team1_id"
  has_many :away_matches, :class_name => Match, :foreign_key => "team2_id"

  validates :name, :uniqueness => true

  def matches
    Match.where("team1_id = ? OR team2_id = ?", self.id, self.id)
  end
end