Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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:destroy\u all和update有许多大小_Ruby On Rails_Rails Activerecord - Fatal编程技术网

Ruby on rails Rails:destroy\u all和update有许多大小

Ruby on rails Rails:destroy\u all和update有许多大小,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我有以下关系: class Match has_many :players end 在一个例子中,我有一场有两名球员的比赛,一名球员的属性为team=1,另一名球员的属性为team=2 在我调用match.players.whereteam:1.destroy_all之后,下面执行的代码使用match.players.size 但是,现在的大小不是2,而是3。如何更新“大小”属性?我不能调用Matk.Read,因为在这个执行过程中对这个对象做了其他的修改, ,你可以考虑使用反高速缓存来实现

我有以下关系:

class Match
  has_many :players
end
在一个例子中,我有一场有两名球员的比赛,一名球员的属性为team=1,另一名球员的属性为team=2

在我调用match.players.whereteam:1.destroy_all之后,下面执行的代码使用match.players.size 但是,现在的大小不是2,而是3。如何更新“大小”属性?我不能调用Matk.Read,因为在这个执行过程中对这个对象做了其他的修改,

,你可以考虑使用反高速缓存来实现这个,所以你的播放器模型得到:

class Player < ApplicationRecord
  belongs_to :match, dependent: :destroy, counter_cache: true
end
这样,当玩家从比赛中添加/删除时,计数器缓存列将自动更新

更多信息请点击这里

add_column :matches, :player_count, :integer