以前Brand類 has_and_belongs_to_many :categories, index: true
如今在Category類 增長
has_and_belongs_to_many :brands, index: true
實現brand.categories關聯的數據 同步到 cagegory.brands
我寫的代碼:
Brand.each do |brand|
if brand.categories
brand.categories.each do |category|
category.brands << brand
category.save
end
end
end
力哥的代碼:
Category.each do |c|
c.brand_ids = Brand.where(category_ids: c.id).collect(&:id)
c.save
end
大神菜鳥高下立判