0

我是新手,希望对如何解决此问题提供一些帮助。我试图按照 DataMapper 中的文档进行操作,但没有通过 Google 找到任何关于如何销毁对象及其相应链接的解决方案。以下是关键代码段。

该模型

class Entity

包括 DataMapper::Resource

属性:entity_id,序列属性:full_legal_name,字符串属性:tax_id,字符串属性:phone_number,字符串属性:fax_number,字符串属性:cell_number,字符串属性:email,字符串,:unique => true,:format =>:email_address 属性: alt_email,字符串属性:is_active,布尔属性:created_at,日期时间属性:created_by,字符串属性:updated_at,日期时间属性:updated_by,字符串属性:auto_pay,布尔属性:use_ach,布尔属性:前缀,字符串属性:first_name,字符串属性: middle_name,字符串属性:last_name,字符串属性:后缀,字符串属性:referral_code,字符串属性:login_name,字符串,:unique => true 属性:hashed_pa​​ssword,字符串,:length => 200 属性:salt,字符串属性:permission_level,整数属性:title,字符串属性:问候,字符串属性:首选名称,字符串属性:preferred_language,字符串属性:security_question,字符串属性:security_answer,字符串属性:signature_font,字符串属性:auth1_checkbox,布尔属性:auth2_checkbox,布尔属性:auth3_checkbox,布尔属性:auth4_checkbox,布尔属性:auth5_checkbox,布尔属性:auth6_checkbox,布尔属性:digital_signature,字符串属性:date_signed,日期时间属性:signatory_ip,字符串属性:signatory_title,字符串

有 n, :addresses, :through => 资源有 n, :aches 有 n, :creditcards

结尾

类人<实体

属性:生日,字符串属性:drivers_license_number,字符串属性:state_issuing_drivers_license,字符串

结尾

类公司<实体

属性:dba_name,字符串属性:legal_structure,字符串属性:url,字符串,:格式=>:url

结尾

班级地址

包括 DataMapper::Resource

属性:address_id,串行属性:esid,字符串,:unique => true 属性:描述,字符串属性:address_line1,字符串属性:address_line2,字符串属性:city,字符串属性:state,字符串属性:zipcode,字符串属性:country,字符串属性:meter_number,字符串属性:meter_type,字符串属性:meter_status,字符串属性:meter_status_date,DateTime 属性:updated_by,字符串属性:switch_indicator,字符串属性:switch_type,字符串属性:selected_switch_date,日期属性:under_contract,布尔属性:contract_end_date,日期

有 n, :entities, :through => 资源

结尾

对象创建和持久性

     if session[:base_route].to_s == "residential"
   #residential processing
   @entity_id = session[:this_person].inspect
   @person = Person.get(@entity_id.to_i)
   @address = Address.new()
   @address.esid = params[:post][:esid]
   @address.description = params[:post][:service_location_description]
   @address.address_line1 = params[:post][:service_address_line1]
   @address.address_line2 = params[:post][:service_address_line2]
   @address.city = params[:post][:service_city_name]
   @address.state = params[:post][:service_state_name]
   @address.zipcode = params[:post][:service_zip_code]
   @address.switch_indicator = params[:post][:switch_indicator]
   @address.switch_type = params[:post][:switch_type]
   params[:post][:under_contract_checkbox]  == "on" ? @under_contract = true : @under_contract = false 
   @address.under_contract = @under_contract

   @person.addresses << @address

   if @person.save

尝试的对象和链接破坏(链接被破坏,但地址破坏不起作用)

    @entity_id = session[:this_person].inspect
    address = Address.get(params[:post][:submit].to_i)
    address.destroy
    entity = Entity.get(@entity_id)
    link = entity.address_entities.get(@entity_id, params[:post][:submit])
    link.destroy      

非常感谢任何提示或建议。

4

1 回答 1

0

答案对象的删除失败,因为答案对象和实体对象之间存在链接。通过颠倒顺序并首先删除链接然后删除对象,对象被销毁。

@entity_id = session[:this_person].inspect
entity = Entity.get(@entity_id)
link = entity.address_entities.get(@entity_id, params[:post][:submit])
link.destroy
address = Address.get(params[:post][:submit].to_i)
address.destroy
于 2012-07-05T13:58:13.250 回答