# Finding record between 2 dates

**URL:** <https://www.ruby-forum.com/t/finding-record-between-2-dates/165026>\
**Category:** Rails\
**Created:** [April 8, 2009, 11:51am UTC](https://www.ruby-forum.com/t/finding-record-between-2-dates/165026 "2009-04-08T11:51:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaibhav\_darji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a4c791/32.png) [@vaibhav\_darji](https://www.ruby-forum.com/u/vaibhav_darji)\
**Post date:** [April 8, 2009, 11:51am UTC](https://www.ruby-forum.com/t/finding-record-between-2-dates/165026/1 "2009-04-08T11:51:52Z")

</div>

I am very new to ruby on rails programming,  
I am using the oracle as my database,

In my project there r 2 text box fields and in first and second text  
boxes i want the date field to be enter by the client and want to  
display the record between those 2 dates how can i do the same, plz be  
more specific

Thanks in advance,  
Vaibhavi…

Not so good not so bad

---

<div class="post-metadata">

**Author:** ![vaibhav\_darji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a4c791/32.png) [@vaibhav\_darji](https://www.ruby-forum.com/u/vaibhav_darji)\
**Post date:** [April 8, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/finding-record-between-2-dates/165026/2 "2009-04-08T16:57:56Z")

</div>

You can do this very easily with a named scope in your model:

named\_scope :between, lambda {|start\_date, end\_date| {:conditions =\>  
[“created\_at BETWEEN :start AND :end”, {:start =\> start\_date, :end =\>  
end\_date}]}}

On Apr 8, 5:51 pm, Vaibhav Deshpande \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![vaibhav\_darji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a4c791/32.png) [@vaibhav\_darji](https://www.ruby-forum.com/u/vaibhav_darji)\
**Post date:** [April 8, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/finding-record-between-2-dates/165026/3 "2009-04-08T18:07:25Z")

</div>

Vaibhav Deshpande wrote:

> I am very new to ruby on rails programming,  
> I am using the oracle as my database,
> 
> In my project there r 2 text box fields and in first and second text  
> boxes i want the date field to be enter by the client and want to  
> display the record between those 2 dates how can i do the same, plz be  
> more specific

I’m not sure if this is an issue or not, but note that Oracle’s DATE  
field is also used for storing DATETIME. Maybe the Rails Oracle adaptor  
handles this properly, but I just wanted to mention it in case you see  
weird behavior.

> I am using the oracle as my database,

P. S. I wish my application could use “The Oracle” as its database. It  
would always get the correct response even if it doesn’t know what to  
ask. 😉

[http://www.imdb.com/character/ch0000765/](http://www.imdb.com/character/ch0000765/)

---

<div class="post-metadata">

**Author:** ![vaibhav\_darji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a4c791/32.png) [@vaibhav\_darji](https://www.ruby-forum.com/u/vaibhav_darji)\
**Post date:** [April 9, 2009, 10:34am UTC](https://www.ruby-forum.com/t/finding-record-between-2-dates/165026/4 "2009-04-09T10:34:26Z")

</div>

Robert W. wrote:

> Vaibhav Deshpande wrote:
> 
> > I am very new to ruby on rails programming,  
> > I am using the oracle as my database,
> > 
> > In my project there r 2 text box fields and in first and second text  
> > boxes i want the date field to be enter by the client and want to  
> > display the record between those 2 dates how can i do the same, plz be  
> > more specific
> 
> I’m not sure if this is an issue or not, but note that Oracle’s DATE  
> field is also used for storing DATETIME. Maybe the Rails Oracle adaptor  
> handles this properly, but I just wanted to mention it in case you see  
> weird behavior.
> 
> > I am using the oracle as my database,
> 
> P. S. I wish my application could use “The Oracle” as its database. It  
> would always get the correct response even if it doesn’t know what to  
> ask. 😉
> 
> [http://www.imdb.com/character/ch0000765/](http://www.imdb.com/character/ch0000765/)  
> I used @date2ss = Date2.find(:all, :conditions =\> “ID \> '”+ @date1 +“’  
> and ID \<'”+ @date2+“'”) and it works…  
> thanks,  
> Vaibhavi

---

<div class="post-metadata">

**Author:** ![vaibhav\_darji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a4c791/32.png) [@vaibhav\_darji](https://www.ruby-forum.com/u/vaibhav_darji)\
**Post date:** [April 9, 2009, 11:00am UTC](https://www.ruby-forum.com/t/finding-record-between-2-dates/165026/5 "2009-04-09T11:00:25Z")

</div>

> ## I used @date2ss = Date2.find(:all, :conditions =\> “ID \> '”+ @date1 +“’ and ID \<'”+ @date2+“'”) and it works… thanks, Vaibhavi

You can also use  
Model.find(:all, :conditions =\> {:date =\> date1…date2})

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![vaibhav\_darji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a4c791/32.png) [@vaibhav\_darji](https://www.ruby-forum.com/u/vaibhav_darji)\
**Post date:** [April 9, 2009, 11:41am UTC](https://www.ruby-forum.com/t/finding-record-between-2-dates/165026/6 "2009-04-09T11:41:46Z")

</div>

Andrew T. wrote:

> > ## I used @date2ss = Date2.find(:all, :conditions =\> “ID \> '”+ @date1 +“’ and ID \<'”+ @date2+“'”) and it works… thanks, Vaibhavi
> 
> You can also use  
> Model.find(:all, :conditions =\> {:date =\> date1…date2})
> 
> Andrew T.  
> [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> 
> “I have never let my schooling interfere with my education” - Mark Twain

Thanks,
