Topic: Scaffold that shows results and destroys them straight away

Hi guys,
I have developed a search facility for my app. However, I don't want the searches to clutter my application.
How would you rewrite a simple scaffold to make it only show and automatically destroy afterwards (when you leave the page)?
I tried .destroy or .delete but it doesn't delete the "search_id"

Thanks

My simple scaffold:

class SearchesController < ApplicationController
  def new
    @search = Search.new
  end
 
  def create
    @search = Search.create!(params[:search])
    redirect_to @search
  end
 
  def show
    @search = Search.find(params[:id])
  end
end

Last edited by Vierka (2012-08-25 12:29:25)

Re: Scaffold that shows results and destroys them straight away

You can't for many reasons. Here's just one
Because you won't know necessarily when a user just closes a pge tab in their browser after running a search

There are a couple of ways to solve your problem
Have all actions in all controllers automaticaly clean up your searches before running by setting up a before filter in your application controller.
This is a totally inefficient way of solving your problem.

Much better way
write a simple rake task to clean up your data. You then have many options you can use as to when you wish to call this. A pretty standard way would be to set up a cron job on your web server to run eery 24 hours that will call the rake task

Heres a nice guide on rake tasks
http://www.stuartellis.eu/articles/rake/
Looks complicated but it's dead simple a small excerpt from the above link

The actual code for the task to run must be enclosed in a standard Ruby do…end block. This is the structure of a simple task:

desc "One line task description"
task :name_of_task do
  # Your code goes here
end
What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)