Forum: Ruby on Rails CHECK if param exists? please help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-02-22 19:34
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!!
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-22 20:00
(Received via mailing list)
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()?
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-22 20:11
(Received via mailing list)
>    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...
D93ae163042e0a937941e0ba4d8ed5d7?d=identicon&s=25 Eric (Guest)
on 2009-02-22 20:43
(Received via mailing list)
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

On Feb 22, 10:34 am, Dave Smith <rails-mailing-l...@andreas-s.net>
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-23 00:48
(Received via mailing list)
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.
D93ae163042e0a937941e0ba4d8ed5d7?d=identicon&s=25 Eric (Guest)
on 2009-02-23 02:15
(Received via mailing list)
On Feb 22, 3:46 pm, Phlip <phlip2...@gmail.com> wrote:
> 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.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-23 03:27
(Received via mailing list)
>> 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!
This topic is locked and can not be replied to.