views:

57

answers:

4

Hello i have a rails app that handles sales, right now, what i need is to be able to delete the sale in order to keep accounting clear, but log somewhere else, the details of that record.

I am thinking i may need to create a logger, but have no idea how, or maybe another object who gets created on the destroy of the sale.

Thanks in advance

+2  A: 

Just an idea - you could add a column to your current table that would act as a "deleted" flag (I've always called this a logical delete). Then you could add a default scope to filter out "deleted" records and add some named scopes that would include the "deleted" records when you need them.

Andy Gaskell
that will be something i would definitively should have tried earlier on development, being a sales app i have already constructed a bunch of code to get reports and the such. this would make me rewrite like 60% of the app, just to filter the flagged as deleted records. thanks.
Carlos Barbosa
+1  A: 

acts as paranoid is a plugin that will handle this for you, but if there's anything about it you don't like you can roll your own version like Andy suggested, maybe with a deleted_at timestamp. You might try overriding the destroy action on the model - I haven't tried it myself, but something like this should work:

class Sale < ActiveRecord::Base
  def destroy
    update_attributes(:deleted_at => Time.now)
  end
end
PreciousBodilyFluids
A: 

Like you said, you could create another object/model that you create a new instance of each time a Sale is deleted. Name it SaleRecord or SaleHistory... something like that. Then you can retrieve these records and do whatever. Then, a nice use case would be to look up sales records for a particular product to calculate statistics on how popular it was...

kchau
i ended up doing this, let's see how it scales, and i have a lot of reports going on, including most sold products and most sold product within a brand. not shure how to get how popular. but thanks bro, i ended up doing this and creating a model called deleted_sales
Carlos Barbosa
A: 

i ended up creating a delete_sale object, and then on my sale observer i created and populated with the data of the sale just before it was destroyed.

  @delsale = DeletedSale.create


   @last_deleted_sale = DeletedSale.find(:last)

    ActiveRecord::Base.connection.execute "UPDATE deleted_sales SET name = #{@venta.id} WHERE id = #{@last_deleted_sale.id};"


  @delsale.update_attributes :cliente => @venta.cliente.name
     @delsale.update_attributes :vendedor => @venta.vendedor.name

     @delsale.update_attributes :instalador => @venta.instalador.name
     @delsale.update_attributes :precio_de_venta => @venta.precio_de_venta
     @delsale.update_attributes :precio_de_instalacion => @venta.precio_de_instalacion
      if @venta.producto_id?
     @delsale.update_attributes :producto_id => @venta.producto_id
    end
     if @venta.cart_id?
     @delsale.update_attributes :cart_id => @venta.cart_id
    end
Carlos Barbosa