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.
Dave S. (Guest)
on 2009-01-20 00: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.
Tranquiliste (Guest)
on 2009-01-20 00: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
Dave S. (Guest)
on 2009-01-20 00: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
Dave S. (Guest)
on 2009-01-20 01: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?
Dave S. (Guest)
on 2009-01-20 01:10
sorry... @articles.in_period(@start_date, @end_date)
Dave S. (Guest)
on 2009-01-20 01:40
Dave S. wrote:
>
> sorry... @articles.in_period(@start_date, @end_date)

anyone have any idea?
Norm (Guest)
on 2009-01-20 19:12
(Received via mailing list)
Dave S. 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")
Dave S. (Guest)
on 2009-02-22 18:24
Norm wrote:
> Dave S. 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
Norm (Guest)
on 2009-02-24 00:26
(Received via mailing list)
Dave S. 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.