店铺按分类随机导入评论

category = Category.find_by(:name => "运动")
category.brands.each do |brand|
  if brand.shops
    brand.shops.each do |shop|
      5.times{
       comment = shop.comments.new
       comment.created_at = rand((Time.now-2592000)..Time.now)
       comment.user = User.where(:robot => true).sample
       com = category.comment_stores.sample
       if  !shop.comment_stores.include?(com)
       comment.content = com.content
       else
       comment.content = category.comment_stores.sample.content
       end
       comment.save
       }
    end
  end
end

原文地址:https://www.cnblogs.com/wangyuyu/p/3456374.html