Search code examples
rubyruby-datamapper

How to destroy an many-to-many object and its link in DataMapper/Sinatra/Ruby


I am a newbie and would appreciate some help on how to resolve this problem. I have tried to follow the documentation in DataMapper and have not found any solutions via Google on how to destroy an object and its corresponding links. Here are the key code segments.

The Model

class Entity

include 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 => :email_address 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 => true property :hashed_password, 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, Boolean property :auth4_checkbox, Boolean property :auth5_checkbox, Boolean property :auth6_checkbox, Boolean property :digital_signature, String property :date_signed, DateTime property :signatory_ip, String property :signatory_title, String

has n, :addresses, :through => Resource has n, :aches has n, :creditcards

end

class Person < Entity

property :birthdate, String property :drivers_license_number, String property :state_issuing_drivers_license, String

end

class Company < Entity

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

end

class Address

include DataMapper::Resource

property :address_id, Serial property :esid, String, :unique => true 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, Date property :under_contract, Boolean property :contract_end_date, Date

has n, :entities, :through => Resource

end

Object creation and Persistence

     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

Attempted object and link destruction (The link is destroyed, but the address destruction does not work)

    @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      

Any tips or suggestions are greatly appreciated.


Solution

  • The deletion of the answer object failed because there was a link between the answer object and the entity object. By reversing the order and deleting the link first and the object second, the object is destroyed.

    @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