Forum: Ruby on Rails date filter

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-01-19 23:21
anyone know any examples of good code for a date range filter i can add
to my app to return only selected articles within that range based on
their created at date.
C7ce3af7050f4580f56654aa6d52a36e?d=identicon&s=25 Tranquiliste (Guest)
on 2009-01-19 23:28
(Received via mailing list)
Hello

You could use named_scope (starting with rails 2.1).

There is a good rails cast presenting them with an example with
dates : http://railscasts.com/episodes/108-named-scope
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-01-19 23:59
Tranquiliste wrote:
> Hello
>
> You could use named_scope (starting with rails 2.1).
>
> There is a good rails cast presenting them with an example with
> dates : http://railscasts.com/episodes/108-named-scope

Hi, thanks for the response.

I am wondering how to do this in my app. I can see the example code

  named_scope :recent, lambda { |time| { :conditions => ["created_at >
?", time] } }

and examples on the net state the same, but I want to be able to input
two dates into boxes and call the filter from the view page, which I
also am not sure how to do.

any help would be greatly appreciated.

dave
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-01-20 00:05
>
> and examples on the net state the same, but I want to be able to input
> two dates into boxes and call the filter from the view page, which I
> also am not sure how to do.
>
> any help would be greatly appreciated.
>
> dave

im pretty sure this would work. But am wondering how I can call this
filter from my page. so for example have two text boxes and a button
that calls somthing similar to "
@article.comments.in_period(@start_date, @end_date)"

  named_scope :in_period, lambda { |start_date, end_date|
     { :conditions => ["articles.created_at >= ? and " +
                       "articles.created_at <= ?",
                       start_date, end_date] }
any ideas?
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-01-20 00:10
sorry... @articles.in_period(@start_date, @end_date)
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-01-20 00:40
Dave Smith wrote:
>
> sorry... @articles.in_period(@start_date, @end_date)

anyone have any idea?
46f52c33235283752423837b91c581dd?d=identicon&s=25 Norm (Guest)
on 2009-01-20 18:12
(Received via mailing list)
Dave Smith wrote:
> anyone know any examples of good code for a date range filter i can add
> to my app to return only selected articles within that range based on
> their created at date.
>

Here is how I fetch something similar.  It is just a simple find.
Substitute the created_at field and it should work.  Is that what you
are looking for?

 @res = Reservation.find(:all,
                            :conditions => [ "enddate >= ? and startdate
<= ?",Date.today,Date.today+120],
                            :order => "space_id,startdate ASC")
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-02-22 17:24
Norm wrote:
> Dave Smith wrote:
>> anyone know any examples of good code for a date range filter i can add
>> to my app to return only selected articles within that range based on
>> their created at date.
>>
>
> Here is how I fetch something similar.  It is just a simple find.
> Substitute the created_at field and it should work.  Is that what you
> are looking for?
>
>  @res = Reservation.find(:all,
>                             :conditions => [ "enddate >= ? and startdate
> <= ?",Date.today,Date.today+120],
>                             :order => "space_id,startdate ASC")

ok thats great but how do I initiate that from the code. so for example
the page will load index and collect the articles normally. but how do i
then get it to collect the articles using the date filter code here!?
sorry for such a noob question
46f52c33235283752423837b91c581dd?d=identicon&s=25 Norm (Guest)
on 2009-02-23 23:26
(Received via mailing list)
Dave Smith wrote:
>> Substitute the created_at field and it should work.  Is that what you
> then get it to collect the articles using the date filter code here!?
> sorry for such a noob question
>
I am not sure I understand what you are trying to do.  Are you trying to
collect a set of information and then letting the user specify some
conditions (date range) so you would then display only those objects
that meet that condition?  If that is what you are doing you could treat
it as two separate actions in the controller each of which will do the
appropriate find from which you then render using the same view.  There
might be another way to do it but I would probably first do it that way
and worry about optimizing when that becomes important.

Sorry if I misunderstand your problem.

Norm
This topic is locked and can not be replied to.