CHECK if param exists? please help

hi folks.

i want to check if a param exists if not to set it to a default value for my date range filter. in my controller i have:

  @from_date = Date.strptime(params[:startdate],"%d/%m/%Y")     @to_date = Date.strptime(params[:enddate],"%d/%m/%Y")

    @articles = @results.find(:all,                             :conditions => [ "created_at >= ? and created_at <=?",@from_date,@to_date],                             :order => 'created_at desc', :page => {:size => 5, :current => params[:page]})

this works as long as i have the params in the query string in the address. but if i remmove them i want to be able to default to a date range from today back a couple of months.

is there a param[:startdate].exists? check i can do???

any help asap would be great!!

First, some code style tips...

> @from_date = Date.strptime(params[:startdate],"%d/%m/%Y") > @to_date = Date.strptime(params[:enddate],"%d/%m/%Y")

Don't use @ unless other methods really want to see the variable.

> @articles = @results.find(:all, > :conditions => [ "created_at >= ? and > created_at <=?",@from_date,@to_date], > :order => 'created_at desc', :page => {:size > => 5, :current => params[:page]})

And always prefer an AR shortcut if you can find one:

   @articles = @results.find_all_by_created_at( from .. to,                     :order => 'created_at desc',                      :page => {:size => 5, :current => params[:page]})

> this works as long as i have the params in the query string in the > address. but if i remmove them i want to be able to default to a date > range from today back a couple of months.

The big sloppy way would be:

   from = 2.months.ago      to = Time.now.to_date    from_date = Date.parse(params[:startdate]) if params[:startdate]      to_date = Date.parse(params[:enddate]) if params[:enddate]

> is there a param[:startdate].exists? check i can do???

params.key?(:startdate). But... hash[q] returns nil (usually) if no q key exists, so everyone always just treats hash[q] as a boolean.

New question: Could someone make those statements even DRYer? such as with .fetch()?

   from_date = Date.parse(params[:startdate]) if params[:startdate]      to_date = Date.parse(params[:enddate]) if params[:enddate]

I forgot to mention I don't know if there's a Date.parse. Use Time.parse otherwise...

Use the ternary/comparison operator. I've removed your strptime stuff, but the idea is like so:

@from_date = params[:startdate] ? params[:startdate] : (Time.now - 3.months.ago) @to_date = params[:enddate] ? params[:enddate] : Time.now

"If params[:startdate], then @from = params[:startdate], else @from = time.now - 3.months.ago"

-eric

Eric wrote:

Use the ternary/comparison operator. I've removed your strptime stuff, but the idea is like so:

@from_date = params[:startdate] ? params[:startdate] : (Time.now - 3.months.ago)

3.months.ago is an absolute time (probably "seconds since 1970"). So the "Time.now -" is implied.

It was just for illustration.

3.months.ago is an absolute time (probably "seconds since 1970"). So the "Time.now -" is implied.

It was just for illustration.

You gotta consider the audience, dude!