这次的要求是要写一个完整的model以及当数据有更新的时候,将这些数据插入到一个新的表中.
class Icm::ReportSummary < ActiveRecord::Base
self.table_name = :icm_report_summaries
end
接下来是写创数据库表的语句.由于有几十列,就不一一写了.
class ReportSummary < ActiveRecord::Migration
def change
create_table :icm_report_summaries do |t|
t.string "request_number", :limit => 32, :collate => "utf8_bin"#单号
t.string "title", :limit => 50, :collate => "utf8_bin"#标题
end
接下来要在对应的model下写回调方法
after_update :monitoring_fields_change
当这个model触发完update的时候会执行这个方法
需要得到他当时的id,通过id得到这个值
def monitoring_fields_change
if self.incident_request_id.present?//self可以得到当前对象
request_number = Icm::IncidentRequest.find_by_id(self.incident_request_id)[:request_number]
report_datas=Icm::ReportSummary.report_detail_data(request_number)//report_detail_data这个方法是之前写好的,获得需要的全部数据
@all=report_datas[:datas][0]//获得request_number
if @all[3].present?
status_id = Icm::IncidentStatusesTl.where(:name=>@all[3], :language=>"zh").first.incident_status_id
@all[3] = Icm::IncidentStatus.where(:id => status_id).first.incident_status_code
end
end
end
当把所有数据都修改完之后,就进行新增操作,把对应的值插入到对应的表中
if @all.present?
Icm::ReportSummary.create(:request_number=>@all[0],:title=>@all[1],
:summary=>@all[2],:incident_status_code=>@all[3],:category_one_code=>@all[4],
:category_two_code=>@all[5],:category_three_code=>@all[6],:category_four_code=>@all[7],
:report_source_code=>@all[8],:fault_category_code=>@all[9],:fault_sub_category_code=>@all[10],
:urgence_code=>@all[11],:impact_range_code=>@all[12],:priority_id_code=>@all[13],
:submitted_person_id_label=>@all[14],:submitted_date=>@all[15],:contact_full_name=>@all[16],
:engineer_number=>@all[17],:niu_vip_flag=>@all[18],:contact_mail=>@all[19],
:contact_number=>@all[20],:location_description=>@all[21],:cent_description_code=>@all[22],
:department_desciption_code=>@all[23],:contact_address => @all[24], :reopen_count => @all[25],
:support_person_name => @all[26],:support_group_code => @all[27], :assign_person_name => @all[28],
:assign_group_code => @all[29],:assign_at => @all[30],:last_assign_at => @all[31],
:assign_spend => @all[32],:assign_sla => @all[33],:assign_count => @all[34],
:respond_person_name => @all[35],:respond_group_name => @all[36],:respond_time => @all[37],
:last_respond_at => @all[38],:respond_spend => @all[39],:respond_sla => @all[40],
:respond_type => @all[41],:first_respond_result => @all[42],:pass_count => @all[43],
:upgrade_count => @all[44],:solve_group_code => @all[45],:solve_person_name => @all[46],
:solve_at => @all[47], :solve_spend => @all[48], :solve_sla => @all[49],
:solve_category_code => @all[50],:solve_sub_category_code => @all[51], :solve_message => @all[52],
:hang_spend => @all[53],:visit_person_name => @all[54],:first_visit_at => @all[55],
:visit_at => @all[56], :visit_message => @all[57],:close_person_name => @all[58],
:close_at => @all[59], :rating_code => @all[60], :rating_reason => @all[61],
:rating_at => @all[62], :other_request_number => @all[63], :process_str => @all[64],
:engineer_department_code => @all[65], :engineer_team_code => @all[66],:solve_person_code => @all[67])
将所有数据插进去
object=Icm::IncidentRequest.find_by_id("001")
boolean=object.update_attribute(:solve_flag,'Y')
这是更新数据的语句