jeudi 30 mars 2017

Ruby: undefined method `delete'

I'm a newbie programmer, basically I'm trying to delete a record from a database table, however this record would be used on a child table with a composed primary key taking both from different tables.
This is the class of the table I need to delete the child data from.

class Detalle_Mapa 
  include DataMapper::Resource
  storage_names[:default] = 'detalle_ma'

  property :clave_mapa, String, :length => 2, :required => true, :key => true
  property :clave_asig, String, :length => 10, :required => true, :key => true
  property :clave_cuatri, String, :length => 2, :required => true

  belongs_to :mapa_curricular, 'Mapa_Curricular', :child_key => [:clave_mapa], :key => true
  belongs_to :cuatrimestre, 'Cuatrimestre', :child_key => [:clave_cuatri], :key => true
  belongs_to :asignatura, 'Asignatura', :child_key => [:clave_asig]

end

So now when I try to delete data from the parent table, it won't delete it since the PK is being used on the child table for other data. If this was normal SQL sintax it wouldn't be a big deal but I'm having a hard time finding a way around it on Ruby.

This is the basic method that works when deleting data that's not being used as a FK.

delete '/deleteMapCurricular/:clave_mapa' do
  @mapa = Mapa_Curricular.get(params[:clave_mapa])
  if @mapa      
     @mapa.destroy
     redirect '/catMapCurricular'
  end    
end

And this is one of the ways I've tried to delete the child data, which is clearly not right...

     Detalle_Mapa.where(:clave_mapa => [params[:clave_mapa]]).delete_all

Is there an easy way to just delete a bunch of data from a database that I'm not aware of? Or what I'm I not getting about this ActiveRecords on Ruby?

I'm sorry if the question is ambiguous or if I'm not explaining myself clearly, I usually find everything on forums and there is no need to ask myself. Any help will be greatly appreciated :)

Aucun commentaire:

Enregistrer un commentaire