Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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 使用的Rails属于同一类,并且与同一类有一个关联_Ruby_Activerecord_Sinatra_Sinatra Activerecord - Fatal编程技术网

Ruby 使用的Rails属于同一类,并且与同一类有一个关联

Ruby 使用的Rails属于同一类,并且与同一类有一个关联,ruby,activerecord,sinatra,sinatra-activerecord,Ruby,Activerecord,Sinatra,Sinatra Activerecord,以下是我制作的一张图片,直观地描述了我正在寻找的关系: 在图中,我说的是互动,但更具体地说,这些互动是游戏中的杀戮。我们都知道,杀人有凶手,也有受害者。在这种情况下,我们的杀手将被称为玩家,我们的受害者将被称为受害者 鉴于这张图片,下面是一些我想执行的示例查询: Player.find(1).Interactions > [<#Interaction id: 1>, <#Interaction id: 2>, <#Interaction id: 3>]

以下是我制作的一张图片,直观地描述了我正在寻找的关系:

在图中,我说的是互动,但更具体地说,这些互动是游戏中的杀戮。我们都知道,杀人有凶手,也有受害者。在这种情况下,我们的杀手将被称为
玩家
,我们的受害者将被称为
受害者

鉴于这张图片,下面是一些我想执行的示例查询:

Player.find(1).Interactions
> [<#Interaction id: 1>, <#Interaction id: 2>, <#Interaction id: 3>]

Interactions.where(player: Player.find(1))
> [<#Interaction id: 1>, <#Interaction id: 2>, <#Interaction id: 3>]

Interactions.where(victim: Player.find(1))
> [<#Interaction id: 4>]

Player.find(1).Interactions.where(victim: Player.find(2))
> [<#Interaction id: 2>]

Player.find(1).Interactions.count()
> 3

# Player.find(1).Interactions should (ideally) return Interactions where <#Player id: 1> is the player (not victim)

Interactions.all.count()
> 4

Player.all.count()
> 4

Player.find(2).Interactions
> []

Player.find(3).Interactions
> [ <#Interaction id: 4> ]

Player.find(4).Interactions
> []
和我的
播放器
型号:

class Player < ActiveRecord::Base
  has_man :kills
end
classplayer
最后,我认为迁移应该是什么样子:

class Interaction < ActiveRecord::Base
  belongs_to :player
  has_one :victim, :class_name => 'Player'
end
class CreatePlayerAndInteraction < ActiveRecord::Migration[5.0]
  def change
    create_table :players do |t|
      t.string :steam_id
    end

    create_table :interactions do |t|
      t.string :weapon
      t.belongs_to :player, index: true
    end
end
class CreatePlayerAndInteraction
交互
表只需要一个
受害者id
列,然后将
有一个
更改为
属于:受害者,职业名称:玩家

这将起作用,因为
交互
基本上是
Player
Player
的联接表

has_one
表示受害者(
Player
在本例中)的交互id不正确

相反,
交互
属于杀手和受害者

设置为:

class Player
  has_many :kills, class_name: Interaction, foreign_key: :player_id inverse_of: :killer
  has_many :deaths, class_name: Interaction, foreign_key: :victim_id, inverse_of: :victim 
end

class Interaction
  belongs_to :killer, class_name: Player, foreign_key: :player_id, inverse_of: :kills
  belongs_to :victim, class_name: Player, foreign_key: :victim_id, inverse_of :deaths
end

工程师们给了我一个很好的答案,它帮助我解决了这个问题,但它有一点松懈。以下是我用来解决问题的确切解决方案:

# ./models/player.rb
class Player < ActiveRecord::Base
  has_many :interactions
end

# ./models/interaction.rb
class Interaction < ActiveRecord::Base
  belongs_to :player
  belongs_to :victim, class_name: 'Player', foreign_key: 'victim_id'
end

# ./db/migrate/migrate/create_players.rb
class CreatePlayersAndInteractions < ActiveRecord::Migration[5.0]
  def change
    create_table :interactions do |t|
      t.integer :victim_id
      t.belongs_to :player, index: true
    end
  end
end
。/models/player.rb
类播放器
嘿!!就这么简单,谢谢你。如果你回答,我很乐意接受。将
Interaction
更改为
Kill
或者这没有任何意义,而且我的“fluff”是基于此的功能,一个
Player
有杀无杀deathes@engineersmnky谢谢你指出这一点!我修好了。我之所以说有绒毛,是因为死亡不一定是杀人的反面。玩家可以通过其他方式死亡。我想确保这个问题有一个清晰的、赤裸裸的答案。谢谢你的帮助!