Keep record of deleted items
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 somewher开发者_JS百科e 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
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.
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
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...
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
精彩评论