has_and_belongs_to_many 关联的数据 同步

2019-08-22  本文已影响0人  ibob2012

之前Brand类   has_and_belongs_to_many :categories, index: true

现在在Category类 增加

has_and_belongs_to_many :brands, index: true

实现brand.categories关联的数据 同步到 cagegory.brands

Category.each do |c|

  c.brand_ids = Brand.where(category_ids: c.id).collect(&:id)

  c.save

end

上一篇下一篇

猜你喜欢

热点阅读