Rich
Rich

Reputation: 2174

Date condition in default_scope for a Model

I want to ensure that only Movies with a watched date are brought back and ordered by that value in ascending order.

I think I'm fairly close with the following:

default_scope :conditions => { :watched_date => not null },
              :order => 'watched_date ASC'

But I don't know how to add a condition for a non-null date.

Upvotes: 1

Views: 660

Answers (1)

Peter Brown
Peter Brown

Reputation: 51697

Does it work if you change the conditions to this?:

:conditions => 'watched_date IS NOT NULL'

You might want to include the table name incase this is used in joins like so:

:conditions => 'movies.watched_date IS NOT NULL'

Upvotes: 2

Related Questions