Date filter

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.

Hello

You could use named_scope (starting with rails 2.1).

There is a good rails cast presenting them with an example with
dates : #108 named_scope - RailsCasts

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 : #108 named_scope - RailsCasts

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

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. wrote:

sorry… @articles.in_period(@start_date, @end_date)

anyone have any idea?

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”)

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

sorry… @articles.in_period(@start_date, @end_date)

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