如何在DataMapper / Sinatra / Ruby中销毁多对多对象及其链接

时间:2012-07-04 14:37:01

标签: ruby ruby-datamapper

我是新手,非常感谢您如何解决此问题。我试图按照DataMapper中的文档进行操作,并且没有通过Google找到有关如何销毁对象及其相应链接的任何解决方案。以下是关键代码段。

模型

class Entity

包括DataMapper :: Resource

property:entity_id,Serial   property:full_legal_name,String   property:tax_id,String   property:phone_number,String   property:fax_number,String   property:cell_number,String   property:email,String,:unique => true,:format => :电子邮件地址   property:alt_email,String   property:is_active,Boolean   property:created_at,DateTime   property:created_by,String   property:updated_at,DateTime   property:updated_by,String   property:auto_pay,Boolean   property:use_ach,Boolean   property:prefix,String   property:first_name,String   property:middle_name,String   property:last_name,String   property:suffix,String   property:referral_code,String   property:login_name,String,:unique =>真正   property:hashed_pa​​ssword,String,:length => 200   property:salt,String   property:permission_level,Integer   property:title,String   property:greeting,String   property:preferred_name,String   property:preferred_language,String   property:security_question,String   property:security_answer,String   property:signature_font,String   property:auth1_checkbox,Boolean   property:auth2_checkbox,Boolean   property:auth3_checkbox,布尔值   property:auth4_checkbox,布尔值   property:auth5_checkbox,布尔值   property:auth6_checkbox,Boolean   property:digital_signature,String   property:date_signed,DateTime   property:signatory_ip,String   property:signatory_title,String

有n,:地址,:通过=>资源   有n,:疼痛   有n,:信用卡

class Person<实体

属性:birthdate,String   property:drivers_license_number,String   property:state_issuing_drivers_license,String

类公司<实体

property:dba_name,String   property:legal_structure,String   property:url,String,:format => :URL

班级地址

包括DataMapper :: Resource

property:address_id,Serial   property:esid,String,:unique =>真正   property:description,String   property:address_line1,String   property:address_line2,String   property:city,String   property:state,String   property:zipcode,String   property:country,String   property:meter_number,String   property:meter_type,String   property:meter_status,String   property:meter_status_date,DateTime   property:updated_by,String   property:switch_indicator,String   property:switch_type,String   property:selected_switch_date,日期   property:under_contract,Boolean   property:contract_end_date,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      

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

1 个答案:

答案 0 :(得分: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