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!!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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()?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> 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... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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!! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 22, 3:46 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
>> 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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---