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