# Apply more than one condition to find(:all)

**URL:** <https://www.ruby-forum.com/t/apply-more-than-one-condition-to-find-all/117393>\
**Category:** Rails\
**Created:** [October 16, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/apply-more-than-one-condition-to-find-all/117393 "2007-10-16T16:11:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/apply-more-than-one-condition-to-find-all/117393/1 "2007-10-16T16:11:37Z")

</div>

Hey,

I have got an action that should retrieve all rows where the created\_at  
column matches the current date, but I want another condition in there  
saying that the column parent\_id should equal to nil (:parent\_id =\>  
nil). I have been trying all sorts, but don’t get any results. Any  
suggestions?

def new  
@year = Time.now.year  
@month = Time.now.month  
@day = Time.now.day  
@date = ("#{@year}-#{@month}-#{@day}")

@logo = Logo.find(:all, :conditions =\> [“created\_at = ?”, @date])  
end

Cheers

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/apply-more-than-one-condition-to-find-all/117393/2 "2007-10-16T16:22:22Z")

</div>

Logo.find(:all, :conditions =\> {:created\_at =\> @date, :parent\_id =\>  
nil})

Logo.find(:all, :conditions =\> [“created\_at = ? AND parent\_id = ?”,  
Date.today, nil])

On Oct 16, 9:11 am, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/apply-more-than-one-condition-to-find-all/117393/3 "2007-10-16T16:34:04Z")

</div>

On 10/16/07, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @year = Time.now.year  
> @month = Time.now.month  
> @day = Time.now.day  
> @date = (“#{@year}-#{@month}-#{@day}”)
> 
> @logo = Logo.find(:all, :conditions =\> [“created\_at = ?”, @date])  
> end

Well, you can include the parent\_id condition with:

:conditions =\> [“created\_at = ? and parent\_id is null”, @date]

But your date stuff worries me. created\_at is a timestamp, so you’ll  
never get a match (unless the time on the timestamp is 00:00:00). If  
you want all the timestamps for a date, you need to find timestamps in  
a 24-hour range, right?

Something like:

:conditions =\> [“created\_at between ? and ? and parent\_id is null”,  
Date.today, Date.today+1]

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/apply-more-than-one-condition-to-find-all/117393/4 "2007-10-16T16:41:27Z")

</div>

Andrew B. wrote:

> Logo.find(:all, :conditions =\> {:created\_at =\> @date, :parent\_id =\>  
> nil})
> 
> Logo.find(:all, :conditions =\> [“created\_at = ? AND parent\_id = ?”,  
> Date.today, nil])
> 
> On Oct 16, 9:11 am, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks mate did the trick.

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/apply-more-than-one-condition-to-find-all/117393/5 "2007-10-16T16:44:37Z")

</div>

Bob S. wrote:

> On 10/16/07, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Well, you can include the parent\_id condition with:
> 
> :conditions =\> [“created\_at = ? and parent\_id is null”, @date]
> 
> But your date stuff worries me. created\_at is a timestamp, so you’ll  
> never get a match (unless the time on the timestamp is 00:00:00). If  
> you want all the timestamps for a date, you need to find timestamps in  
> a 24-hour range, right?
> 
> Something like:
> 
> :conditions =\> [“created\_at between ? and ? and parent\_id is null”,  
> Date.today, Date.today+1]

Oh no my created\_at field only contains the date and not the time as  
well otherwise it would be a nightmare. Thanks anyways.

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/apply-more-than-one-condition-to-find-all/117393/6 "2007-10-16T16:57:53Z")

</div>

kevin cline wrote:

> On Oct 16, 9:44 am, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Oh no my created\_at field only contains the date and not the time as  
> > well otherwise it would be a nightmare. Thanks anyways.
> 
> Ok, but the RAILS convention is to use ‘created\_on’ for dates and  
> ‘created\_at’ for dates and times.

Ok I didn’t know that thanks for letting me know I’ll have to change  
that then.

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/apply-more-than-one-condition-to-find-all/117393/7 "2007-10-16T16:55:12Z")

</div>

On Oct 16, 9:44 am, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Oh no my created\_at field only contains the date and not the time as  
> well otherwise it would be a nightmare. Thanks anyways.

Ok, but the RAILS convention is to use ‘created\_on’ for dates and  
‘created\_at’ for dates and times.
