Apply more than one condition to find(:all)

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

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. [email protected]

On 10/16/07, Rajeev K. [email protected] 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]

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. [email protected]

Thanks mate did the trick.

Bob S. wrote:

On 10/16/07, Rajeev K. [email protected] 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.

kevin cline wrote:

On Oct 16, 9:44 am, Rajeev K. [email protected]
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.

On Oct 16, 9:44 am, Rajeev K. [email protected]
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs