Ruby on rails 数据库生成前关于表/模型逻辑的问题

Ruby on rails 数据库生成前关于表/模型逻辑的问题,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我正在尝试使用rails进行我的第一个侧面项目学习,你能帮我理解吗 其基本思想是建立一个博彩游戏,其中一个用户生成一个只能被另一个用户接受的新赌注(每个赌注只分配两个竞争者,创建者和其他玩家) 我在考虑两张桌子: 使用者 打赌 通常情况下,我会为创建赌注的用户建立一对多关系。但我对“竞争对手”一栏感到困惑,在该栏中,另一个用户也是具有用户id的用户。如何更好地表达这种关系: 仔细考虑后,它看起来不是一个好的设置,因为我正在重命名一个列,其中存储了用户id,并且使用多对多“通过”模型没有意义,因为

我正在尝试使用rails进行我的第一个侧面项目学习,你能帮我理解吗

其基本思想是建立一个博彩游戏,其中一个用户生成一个只能被另一个用户接受的新赌注(每个赌注只分配两个竞争者,创建者和其他玩家)

我在考虑两张桌子:

  • 使用者

  • 打赌

  • 通常情况下,我会为创建赌注的用户建立一对多关系。但我对“竞争对手”一栏感到困惑,在该栏中,另一个用户也是具有
    用户id
    的用户。如何更好地表达这种关系:

    仔细考虑后,它看起来不是一个好的设置,因为我正在重命名一个列,其中存储了
    用户id
    ,并且使用多对多“通过”模型没有意义,因为它是“只有一个竞争对手可以参与该赌注”

    我想通过“一对一”创建一个“竞争对手”表,如下所示:

    你能给我解释一下如何以更好的方式建造它吗


    非常感谢

    这只是个主意,你可以用两个外键来完成 所以用户可以作为创建者或竞争对手,您还可以确保创建者id和竞争对手id的值不能相同,因为用户不能与self下注

    class Bet < ActiveRecord::Base
      belongs_to :creator,    foreign_key: "creator_id",    class_name: "User"
      belongs_to :competitor, foreign_key: "competitor_id", class_name: "User"
    end
    
    class User < ActiveRecord::Base
      # as creator to create bet
        has_many: creator_bets,    foreign_key: :creator_id, class_name: "Bet"
      # as competitor to create bet
        has_many: competitor_bets, foreign_key: :competitor_id, class_name: "Bet"
    end
    
    @user = User.first
    @user.creator_bets.build(...)
      # this to create bet as creator
    @user.competitor_bets.build(...)
      # this to create bet as competitor
    
    class-Bet
    这只是一个想法,你可以用两个外键来完成 所以用户可以作为创建者或竞争对手,您还可以确保创建者id和竞争对手id的值不能相同,因为用户不能与self下注

    class Bet < ActiveRecord::Base
      belongs_to :creator,    foreign_key: "creator_id",    class_name: "User"
      belongs_to :competitor, foreign_key: "competitor_id", class_name: "User"
    end
    
    class User < ActiveRecord::Base
      # as creator to create bet
        has_many: creator_bets,    foreign_key: :creator_id, class_name: "Bet"
      # as competitor to create bet
        has_many: competitor_bets, foreign_key: :competitor_id, class_name: "Bet"
    end
    
    @user = User.first
    @user.creator_bets.build(...)
      # this to create bet as creator
    @user.competitor_bets.build(...)
      # this to create bet as competitor
    
    class-Bet
    拥有一个多对多的“直通”模型是没有意义的,因为它是一个 “只有一个竞争者可以参与该赌注”

    事实上是的。在许多方面,它比让多个关联指向同一个表更简单,因为您不必处理用户可能位于任何一列的情况,这需要类似以下内容:

    # this gets much messier as you have to deal with more complex problems
    Bet.where('bets.user_id = :id OR bets.competitor_id = :id', id: params[:user_id])
    
    多对多关联还为您提供了一个选项,可以在以后以最少的重新设计来消除该限制

    鉴于以下关联:

    # This represents an event that users can bet on 
    # for example Elon Musk being the first man on mars.
    class Event < ApplicationRecord
      has_many :bets
      has_many :users, through: :bets
    end
    
    class User < ApplicationRecord
      has_many :bets
      has_many :events, through: :bets
    end
    
    # This is the "join model" that joins User and Event
    # columns:
    # - user_id [bigint, fk] 
    # - event_id [bigint, fk] 
    # - amount [decimal]
    class Bet < ApplicationRecord
      belongs_to :user
      belongs_to :event
    end
    
    当然,您可以通过在控制器中添加or将用户数量限制为两个。如果只有两个,则可以假定
    event.users.first
    是创建者,而
    event.users.last
    是竞争对手

    拥有一个多对多的“直通”模型是没有意义的,因为它是一个 “只有一个竞争者可以参与该赌注”

    事实上是的。在许多方面,它比让多个关联指向同一个表更简单,因为您不必处理用户可能位于任何一列的情况,这需要类似以下内容:

    # this gets much messier as you have to deal with more complex problems
    Bet.where('bets.user_id = :id OR bets.competitor_id = :id', id: params[:user_id])
    
    多对多关联还为您提供了一个选项,可以在以后以最少的重新设计来消除该限制

    鉴于以下关联:

    # This represents an event that users can bet on 
    # for example Elon Musk being the first man on mars.
    class Event < ApplicationRecord
      has_many :bets
      has_many :users, through: :bets
    end
    
    class User < ApplicationRecord
      has_many :bets
      has_many :events, through: :bets
    end
    
    # This is the "join model" that joins User and Event
    # columns:
    # - user_id [bigint, fk] 
    # - event_id [bigint, fk] 
    # - amount [decimal]
    class Bet < ApplicationRecord
      belongs_to :user
      belongs_to :event
    end
    

    当然,您可以通过在控制器中添加or将用户数量限制为两个。如果你只有两个,你可以假设
    事件.用户。第一个
    是创建者,最后一个
    是竞争对手。

    属于:创建者,类名:“User”
    就足够了。当可以从关联的名称派生出
    外键时,不需要指定它。
    属于:创建者,类名:“User”
    就足够了。如果可以从关联的名称派生出
    外键
    ,则无需指定该外键。