Search code examples
ruby-on-railsrubyconditional-statementsdefault-scope

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.


Solution

  • 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'