更新db中的克隆元素值,而不删除Previous cloned字段记录

时间:2016-03-22 11:06:30

标签: sql ruby

我克隆了包含3个字段的“div”。我在数据库中更新这些值时,我删除了早期的值并使用id替换这些值。如何在不删除早期记录值的情况下更新列值?

@pre_data=MgMultiSelect.where(:mg_details_id=>params[:id])

@pre_data.each do |select|
  select.is_deleted=1
  select.save
end

@selected_data=params[:team_id_new]

if @selected_data.present?

  for i in 0...@selected_data.length

    @update_data=MgMultiSelect.new()
    @update_data.team_id_new=@selected_data[i]
    @update_data.mg_details_id=@task_data.id
    @update_data.is_deleted=0
    @update_data.save

  end

end

型号:

class CreateMgMultiSelects < ActiveRecord::Migration
def change
  create_table :mg_multi_selects do |t|
     t.string :team_id_new
     t.integer :mg_details_id
     t.boolean :is_deleted
     t.timestamps
    end
 end
end

1 个答案:

答案 0 :(得分:1)

你应该这样试试:

@pre_data=MgMultiSelect.where(:mg_details_id=>params[:id])

@selected_data=params[:team_id_new]
if params[:team_id_new].present?

  params[:team_id_new].each do |id|
   obj = @pre_data.where(:team_id_new => id).first
   if obj.present?
    obj.update_attributes(:mg_details_id => @task_data.id,:is_deleted => 0 )
   end
  end
end