Ruby on rails 为自引用关联创建/更新记录(和关联记录)的更好方法
我需要知道为自引用关联创建/更新记录的更好方法 我有一个称为Ruby on rails 为自引用关联创建/更新记录(和关联记录)的更好方法,ruby-on-rails,ruby,rails-activerecord,Ruby On Rails,Ruby,Rails Activerecord,我需要知道为自引用关联创建/更新记录的更好方法 我有一个称为模板的模型,一个自参考的与这个模型有很多关联: has_many :related_templates, :foreign_key => :parent_id has_many :children, :through => :related_templates, :source => :child has_many :inverse_related_templates, class_name: "RelatedTem
模板的模型
,一个自参考的与这个模型有很多关联:
has_many :related_templates, :foreign_key => :parent_id
has_many :children, :through => :related_templates, :source => :child
has_many :inverse_related_templates, class_name: "RelatedTemplate", :foreign_key => :child_id
has_many :parents, :through => :inverse_related_templates, :source => :parent
这样,RelatedTemplates
就是一个连接模型
对于新的表单:
1) 填写父模板
详细信息
2) 通过以相同的形式创建新模板或将现有模板作为子模板添加到此父模板,添加子模板
对于编辑表单:
1) 显示父模板的详细信息
2) 编辑已分配给它的子模板的详细信息。用户还应该能够编辑已关联模板的详细信息
因为“新建”和“编辑”的表单相同。用户还可以在编辑页面中从现有的创建一个新的或添加
我的create
方法如下:
def create
Template.transaction do
parent_template = Template.new template_params
if parent_template.save
children_templates = []
# if params of new templates are sent
if params[:template][:children_attributes].present?
new_templates = params[:template][:children_attributes]
new_templates.each do |child|
template = Template.new(attrs)
template.save
children_templates << template
end
end
# if params of existing templates are sent
if params[:template][:existingChildTemplates].present?
existing_ids = params[:template][:existingChildTemplates]
children_templates << Template.where(id: existing_ids)
end
parent_template.children << children_templates
render json: { success: true, template_id: parent_template.id, message: "Created Successfully" }
else
render_422 parent_template, 'Could not save Parent Template.'
end
end
rescue ActiveRecord::RecordInvalid => exception
render json: { success: false, message: "#{exception}" }, status: 500
end
问题
如何白名单子项属性?这里我手动使用参数
。我尝试在permit方法中添加children\u属性:[]
,并在模型中为:children
接受嵌套的属性
但它仍然是不允许的参数
编写创建/更新
方法的更好方法应该是什么?我应该如何处理验证
错误
def update
if @template.update_attributes template_params
if params[:template][:children_attributes].present?
associated_templates = @template.children
new_templates = params[:template][:children_attributes]
new_templates.each do |child|
unless child[:id].present?
# if new template added
associated_templates.build(attrs)
@template.save
else
# if template attributes updated
new_template_params = child.permit(attrs)
child_template = associated_templates.where(id: child[:id]).first
if (child_template[:name] != child[:name]) || (child_template[:role_id] != child[:role_id])
child_template.update_attributes(new_template_params)
end
end
end
end
if params[:template][:existingChildTemplates].present?
children_templates = []
# if selected from the existing one
existing_ids = params[:template][:existingChildTemplates]
children_templates << Template.where(id: existing_ids)
@template.children << children_templates
end
render json: { success: true, message: 'Updated.' }
else
render_422 @template, 'Template could not be updated.'
end
end