实现根据关联表children表进行parent表对children数量的统计 更新
首先,parent表与children表进行model的关联,然后使用parent.children.size 可以查看数量,但是这样不能进行排序,
所以,进行新的列的创建,parent_child_count,进行migrate
在子表的model中,增加子表创建就更新parent表的数量,每创建一个就增加一。
class NftAsset < ApplicationRecord
has_one :avatar
belongs_to :account, optional: true
after_create do |nft_asset|
account = nft_asset.account
account.nft_asset_count += 1
account.save!
end
end
进入irb环境进行测试
