ruby-on-rails – 为什么我的根节点最后将自己作为带有acts_as_t
发布时间:2020-12-16 20:59:39 所属栏目:百科 来源:网络整理
导读:我有一个现有的树结构,我想添加一个新的根并将现有的根向下移动一个.我写了一个rake任务,除了一件事情以外还可以正常工作. 新的root以一个parent_id结束,该parent_id匹配它的新id而不是NULL.成功更改现有根以将新根作为父级. # Rake taskdesc "Change catego
我有一个现有的树结构,我想添加一个新的根并将现有的根向下移动一个.我写了一个rake任务,除了一件事情以外还可以正常工作.
新的root以一个parent_id结束,该parent_id匹配它的新id而不是NULL.成功更改现有根以将新根作为父级. # Rake task desc "Change categories to use new root" task :make_new_category_root => :environment do Company.all.each do |company| current_roots = company.root_categories new_root = Category.new(name: "New root") new_root.company = company new_root.parent = nil if new_root.save current_roots.each do |current| current.parent = new_root current.save end end end # Category class,abbreviated class Category < ActiveRecord::Base include ActsAsTree acts_as_tree :order => "name" belongs_to :company,touch: true validates :name,uniqueness: { scope: :company_id },:if => :root? scope :roots,where(:parent_id => nil) end 解决方法
我需要确定公司#root_categories,但我预测root_categories实际上包含new_root.
是由于Rails中查询的延迟评估. 尝试改变: current_roots = company.root_categories 至: current_roots = company.root_categories.all (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |